Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django SimpleListFilter

Tags:

python

django

Let's say I have a model like this:

# models.py

class Customer(models.Model):
    customer_name=models.CharField(max_length=50)
    licence_key=models.CharField(max_length=100)
    exp_date=models.DateTimeField(blank=True, null=True)

    def __unicode__(self):
        return self.customer_name

I want to have a custom list filter in admin site that shows customer that their exp_date has been passed. It should have two parameters valid and invalid for example and compare exp_date with today's date and show the result. I've read it can be done by simplelistfilter but I couldn't figure how.

Thank you and sorry for my english

like image 698
Ali Soltani arabshahi Avatar asked Apr 10 '16 09:04

Ali Soltani arabshahi


People also ask

What is list filter in Django admin?

Django allows the user of the admin site to filter the instances of a Model by adding the list_filter attribute to your ModelAdmin objects. You can find more information about the Django's filtering utilities in the official documentation, in the Django Admin Site section.

How do I access my Django admin page?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

How do I change the administrator name in Django?

To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.

Where is Django admin template?

The default templates used by the Django admin are located under the /django/contrib/admin/templates/ directory of your Django installation inside your operating system's or virtual env Python environment (e.g. <virtual_env_directory>/lib/python3. 5/site-packages/django/contrib/admin/templates/ ).


1 Answers

Try the below code. It will provide you to filter with the valid/invalid customers list.

class ExpiryDateFilter(admin.SimpleListFilter):

    title = _('Title name')

    parameter_name = 'exp_date'

   def lookups(self, request, model_admin):
       """
           List of values to allow admin to select
       """
       return (
          ('valid', _('All Valid')),
          ('invalid', _('All Invalid')),
       )

  def queryset(self, request, queryset):
     """
         Return the filtered queryset
     """

    if self.value() == 'valid':
        return queryset.filter(exp_date__gt=datatime.datatime.now())
    elif self.value() == 'invalid':
        return queryset.filter(exp_date__lt=datatime.datatime.now())
    else:
        return queryset

In the admin class add a list filter like the following.

class youModelAdminClass(admin.ModelAdmin):

     list_filter = [ExpiryDateFilter]
     list_display = ['CustomerValidity']

and then register the model admin with you Django model.

admin.site.register(ModelClassName, youModelAdminClass)

Hope this will help.

like image 169
Muhammad Shoaib Avatar answered Oct 17 '22 16:10

Muhammad Shoaib