From the django tutorial was_published_recently.admin_order_field = 'pub_date'

From django tutorial

was_published_recently.admin_order_field = 'pub_date' 

What makes this expression really?

+6
source share
1 answer

This refers to the admin section of django.

In the admin section corresponding to the models, each model has a list_display property that controls which fields are displayed in the change list (list of all objects) of the admin page.

Now, if you want to change the default sort order for was_published_recently to list_display , you can do this 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 add your own column named was_published_recently and indicate that the sort order is the pub_date database pub_date when the sort button for the was_published_recently column is was_published_recently .

This is best understood by admin_order_field to the information related to admin_order_field in this link

+10
source

Source: https://habr.com/ru/post/953329/


All Articles