Django admin foreign key date list filter
I have a model like this
class BaseRequest(models.Model):
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
price_quoted = models.DecimalField(max_digits=10, decimal_places=2,null=True, blank=True)
class RequestLeg(models.Model):
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
depart_date = models.DateField()
num_seats = models.IntegerField()
class Request(BaseRequest):
owner = models.ForeignKey(User, null=True, blank=True)
name = models.CharField(max_length=30, null=True, blank=True)
email = models.EmailField()
phone = models.CharField(max_length=30, null=True, blank=True)
legs = models.ManyToManyField(RequestLeg)
and admin for above. Request Model
class RequestAdmin(NoDeletionsAdmin):
list_display=['id', 'created', 'name', 'depart_date']
def depart_date(self, obj):
try:
return min([leg.depart_date for leg in obj.legs.all()])
except ValueError, e:
return None
I want a Daterangelist filter for date_date in RequestAdmin?
+3
source to share
2 answers
I created my own date range filter for date_date as follows
class DateFieldListFilter(FieldListFilter):
def __init__(self, field, request, params, model, model_admin, field_path):
self.field_generic = '%s__' % field_path
self.date_params = dict([(k, v) for k, v in params.items()
if k.startswith(self.field_generic)])
now = timezone.now()
# When time zone support is enabled, convert "now" to the user time
# zone so Django definition of "Today" matches what the user expects.
if timezone.is_aware(now):
now = timezone.localtime(now)
if isinstance(field, models.DateTimeField):
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
else: # field is a models.DateField
today = now.date()
tomorrow = today + datetime.timedelta(days=1)
self.lookup_kwarg_since = '%s__gte' % field_path
self.lookup_kwarg_until = '%s__lt' % field_path
self.links = (
(_('Any date'), {}),
(_('Today'), {
self.lookup_kwarg_since: str(today),
self.lookup_kwarg_until: str(tomorrow),
}),
(_('Past 7 days'), {
self.lookup_kwarg_since: str(today - datetime.timedelta(days=7)),
self.lookup_kwarg_until: str(tomorrow),
}),
(_('This month'), {
self.lookup_kwarg_since: str(today.replace(day=1)),
self.lookup_kwarg_until: str(tomorrow),
}),
(_('This year'), {
self.lookup_kwarg_since: str(today.replace(month=1, day=1)),
self.lookup_kwarg_until: str(tomorrow),
}),
)
super(DateFieldListFilter, self).__init__(
field, request, params, model, model_admin, field_path)
def expected_parameters(self):
return [self.lookup_kwarg_since, self.lookup_kwarg_until]
def choices(self, cl):
for title, param_dict in self.links:
yield {
'selected': self.date_params == param_dict,
'query_string': cl.get_query_string(
param_dict, [self.field_generic]),
'display': title,
}
FieldListFilter.register(
lambda f: isinstance(f, models.DateField), DateFieldListFilter)
In the Admin request
list_filter = [('legs__depart_date', DateRangeListFilter),]
def lookup_allowed(self, key, value):
if key in ('legs__depart_date__gte', 'legs__depart_date__lt'):
return True
return super(RequestAdmin, self).lookup_allowed(key, value)
+2
source to share
Filter it with a field search , just like any other relationship:
Request.objects.filter(legs__created__range=["2015-01-01", "2016-01-31"])
If filtering by a field from the parent model is required, I suggest reading Multiple Table Inheritance .
A TL; DR will be that Django implicitly creates OneToOneField
in the child model, and we can filter it just like any other field, for example:
BaseRequest.objects.filter(request__depart_date__range=[...])
or
Request.objects.filter(baserequest_ptr__created__range=[...])
However, the second request is usually better written like this, allowing Django to handle internal elements:
Request.objects.filter(created__range=[...])
+2
source to share