我有一个这样的模型
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 过滤器?
最佳答案
我已经按照以下方式为 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)
关于python - 外键 Django 管理的日期范围列表过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31539018/