开发者

Django admin filter on null date

The filter options on django admin for开发者_如何转开发 a date are:

Any date Today Past 7 days This month This year

What is the easiest way to add "None" that filters on blank dates?


I just wrote a custom filter for this purpose. It extends Django admin's default DateTimeListFilter and adds "None" and "Not None" options, which do what you think they do.

https://djangosnippets.org/snippets/10566/

You can then go on to use it:

class MyAdmin(ModelAdmin):
    list_filter = (('my_date_field', DateFieldListFilterOrNull),
                  )


You'll have to register your own filter - look at Custom Filter in Django Admin on Django 1.3 or below or http://twigstechtips.blogspot.com/2010/10/django-create-custom-admin-model-filter.html or lots of other places for how that's done.

Regarding your particular requirement, a snippet for an "IS NULL/IS NOT NULL" filter at http://djangosnippets.org/snippets/1963/


Think you're looking for the __isnull postfix. For example:

>>> mymodels = MyModel.objects.filter(mydate__isnull=True)
>>> mymodels[0].mydate
(no response)
>>>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜