3

I'm using Django 1.2's new ManyToMany admin.TabularInline to display related objects in the admin app, and it works great except I can't figure out what to set the "ordering" property to so it can sort by one of the cross-referenced field names.

For instance:

class Foo(models.Model):
    name = models.CharField(max_length=100)

class Bar(models.Model):
    title = models.CharField(max_length=100)
    foos = models.ManyToManyField(Foo)

class FooBarInline(admin.TabularInline):
    model = Bar.foos.through
    ordering = ('name', )  # DOES NOT WORK
    raw_id_fields = ('name', )  # THROWS EXCEPTION

class FooAdmin(admin.ModelAdmin):
    inlines = (FooBarInline, )

    class Meta:
        model = Foo

How can I get to the Foo.name field to order by it in the inline?

Jough Dempsey
  • 663
  • 8
  • 22

2 Answers2

3

The model ordering meta option designates the order of the inline elements.

class Foo(models.Model):
    name = models.CharField(max_length=100)

    class Meta:
        ordering = ('name',)

If you needed to have the ordering of the admin model different from your primary ordering, you could do something like this:

class Foo_Extended(Foo):
    class Meta:
        ordering = ('name',)

And use Foo_Extended for your AdminInline model.

I'm assuming you know this, but Django 1.3 adds and ordering option to the InlineAdmin model but I know you said Django 1.2

Piper Merriam
  • 2,774
  • 2
  • 24
  • 30
0

I think you may override

ModelAdmin.formfield_for_foreignkey(self, db_field, request, **kwargs)

You can find details in the docs for ModelAdmin.formfield_for_foreignkey.

istruble
  • 13,363
  • 2
  • 47
  • 52
yufeng
  • 1