Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From the django tutorial was_published_recently.admin_order_field = 'pub_date'

Tags:

python

django

From the django tutorial

 was_published_recently.admin_order_field = 'pub_date'

What is this statement really doing?

like image 515
BluePython Avatar asked Sep 06 '13 14:09

BluePython


People also ask

How to create new user Django?

The correct way to create a user in Django is to use the create_user function. This will handle the hashing of the password, etc..

What is default Django admin password?

Run 'python manage.py migrate' to apply them. Username (leave blank to use 'chatru'): admin Email address: [email protected] Password: Password (again): The password is too similar to the username.


1 Answers

This is in reference to the admin section of django.

In the admin section corresponding to models, each models has a property called list_display, which control which fields are displayed on the change list (list display of all the objects) page of the admin.

Now, if you wish to change the default sort order for was_published_recently in the list_display you can do so by setting the admin_order_field attribute.

So, in the example:

class Poll(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

You are adding a custom column called was_published_recently and specifying the sort order to be the database field pub_date when the "sort" option for the column was_published_recently is clicked.

You can understand this better by scrolling down to the information pertaining to admin_order_field in this link

like image 54
karthikr Avatar answered Sep 27 '22 19:09

karthikr