Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin: How do I set the ordering of inline elements?

With a normal ModelAdmin class I can set the ordering with:

ordering = ("field_name",) 

There seems to be no option to set ordering for InlineModelAdmin. Is there a way to get the inline elements to sort by a particular field?

like image 381
postfuturist Avatar asked Aug 11 '10 22:08

postfuturist


People also ask

How do I sort data in Django admin?

When you add a calculated field Django doesn't know how to do a order_by , so it doesn't add sorting capability on that field. If you want to add sorting on a calculated field, you have to tell Django what to pass to order_by . You can do this by setting the admin_order_field attribute on the calculated field method.

What is Inlines in Django?

django-inline-actions provides a handy templatetag render_inline_action_fields , which adds these information as hidden fields to a form. As the action does not know that an intermediate form is used, we have to include some special handling.


2 Answers

This works now so you can just do something like:

class MyModelInline(admin.TabularInline):    model = MyModel    ordering = ("field_name",) 
like image 86
Rick Westera Avatar answered Oct 11 '22 03:10

Rick Westera


There's actually a ticket for this, with a patch here: http://code.djangoproject.com/ticket/13862

Don't know if it actually works, though.


Update: The ticket is marked as fixed.

like image 21
Sævar Avatar answered Oct 11 '22 04:10

Sævar