Well, to answer my own question, it turned out that the Q object was the key to this. It allows me to use a more complex OR statement, rather than the usual AND. So I've ended up with:
class ActiveManager(models.Manager): def get_query_set(self): objects = super(ActiveManager, self).get_query_set() objects = objects.filter(Q(active_from__isnull = True) | Q(active_from__lte = datetime.datetime.today())) objects = objects.filter(Q(active_to__isnull = True) | Q(active_to__gte = datetime.datetime.today())) return objects On 02/03/07, Phil Powell <[EMAIL PROTECTED]> wrote: > I'm writing a custom Manager to only return objects which fall within > dates set for Active From and Active To fields. It currently looks > like this: > > class ActiveManager(models.Manager): > def get_query_set(self): > objects = super(ActiveManager, self).get_query_set() > objects = objects.exclude(active_from__lt = datetime.datetime.today()) > objects = objects.exclude(active_to__gt = datetime.datetime.today()) > return objects > > Here's the gotchya though: if either of those dates aren't set, I want > the object to still be returned. i.e. if Active From isn't set, then > it's date should be treated as the beginning of time. > > An easy fix would be for me to make these required fields, but I'd > quite like them to be optional. Is there any way I can do this > without writing a custom query? > > -Phil > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---