Another option is that if you have a filter that you always want to apply, add a user manager to the model in question, which always applies the filter to the results returned.
A good example of this is the Event model, where for 90% of the queries you make on the model, you need something like Event.objects.filter(date__gte=now) , that is, you usually are interested in Events that are upcoming. It will look like this:
class EventManager(models.Manager): def get_query_set(self): now = datetime.now() return super(EventManager,self).get_query_set().filter(date__gte=now)
And in the model:
class Event(models.Model): ... objects = EventManager()
But again, this applies to the same filter against all default queries executed in the Event model, and therefore some of the methods described above are not so flexible.
mrmagooey Sep 10 '12 at 11:37 2012-09-10 11:37
source share