Login

Tag "querysets"

Snippet List

common model privacy

A BooleanField indicating privacy is common on models, but the name of the field and whether the field being True indicates private or public both may change across models. If there is more than one potentially private model, a common interface is needed. A commonly-named method would do the job with `[a for a in MyModel.objects.all() if not a.is_private()]`, but it would still retrieve private instances from the database only to filter them out. This approach puts the name of the privacy field and whether that field being True indicates private or public in a tuple attribute of the model class. A chainable method is added to all QuerySet objects. example use: class MyModel(models.Model): is_public = models.BooleanField(default=True) # ... privacy_field = ("is_public", False) \>\>\> publicMyModels = MyModel.objects.all().exclude_private() \>\>\> values = publicMyModels.values()

  • models
  • querysets
Read More

1 snippet posted so far.