外键Django admin的日期范围列表过滤器

时间:2015-07-21 12:26:59

标签: python django django-admin

我有这样的模型

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)
上面的请求模型的

和管理员是

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

我想在RequestAdmin中为depart_date过滤Daterangelist? enter image description here

2 个答案:

答案 0 :(得分:2)

使用field lookups过滤它,就像任何其他关系一样:

Request.objects.filter(legs__created__range=["2015-01-01", "2016-01-31"])

如果需要从父模型过滤字段,建议您阅读Multi-table inheritance

TL; DR将是Django在子模型上隐式创建OneToOneField,我们可以像其他任何字段一样过滤它,例如:

BaseRequest.objects.filter(request__depart_date__range=[...])

Request.objects.filter(baserequest_ptr__created__range=[...])

然而,第二个查询通常更好地编写,让Django处理内部:

Request.objects.filter(created__range=[...])

答案 1 :(得分:2)

我已按照以下方式为depart_date创建自定义日期范围过滤器

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's time
            # zone so Django's 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)

在“请求管理员”中

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)