Django — How to Order Inline ForeignKey

To apply ordering to a foreign key in the admin, you simply define an “ordering” attribute in your ModelAdmin.

But what about Inlines?

To apply ordering to an Inline Model’s foreign key, override the default form of the admin.StackedInline or admin.TabularInline subclass by specifying a ModelForm.

class OrderedInlineForm(forms.ModelForm):
    product = forms.ModelChoiceField(queryset=Product.objects.order_by('name'))
    class Meta:
        model = MyInlineModel

class MyInlineModelAdmin(admin.StackedInline):
    model = MyInlineModel
    form = OrderedInlineForm

One thought on “Django — How to Order Inline ForeignKey

  1. hello
    this method is good, but the form remove the ‘add’ jpg,so i can not add one referenced model in pop_win
    so could you add this functionality.
    thank you very much

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s