Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Last active February 26, 2019 14:00
Show Gist options
  • Save kezabelle/07277a93d8e215842a8652c57811e1e0 to your computer and use it in GitHub Desktop.
Save kezabelle/07277a93d8e215842a8652c57811e1e0 to your computer and use it in GitHub Desktop.
For Vincent in Slack
class MyQuerySet(QuerySet):
def is_trial(self):
return self.filter(is_trial=True)
def isnt_trial(self):
return self.filter(is_trial=False)
class MyModel(Model):
trial = BooleanField(default=False)
manager1 = MyQuerySet.as_manager()
manager2 = Manager.from_queryset(MyQuerySet)()
# Usage is then:
# MyModel.manager1.is_trial()
# MyModel.manager2.isnt_trial().filter(pk=5).filter(woof="lol")
# By using a single named method, you make it easier to find where it's intentionally used, vs just trying to find
# all things like .filter(product=1, client=5, is_trial=variable) or .exclude(Q(is_trial=False) | Q(product=1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment