My Models:
OrderInfo is one-to-one with Print, which is a Many-to-One with Painting, which itself is a Many-to-One with Club.
class OrderInfo(models.Model):
print_ordered = models.OneToOneField(Print, blank=True)
class Print(models.Model):
painting = models.ForeignKey(Painting)
class Painting(models.Model):
club = models.ForeignKey(Club)
In my OrderInfoAdmin, I'd like to be able to sort by the related Club, but I can't figure out the syntax to do this.
I tried this, but it does not work:
class OrderInfoAdmin(admin.ModelAdmin):
list_filter = ['print_ordered__painting__club']
Any help appreciated, thanks in advance!
Just today, Josh VanderLinden posted a solution for this on his blog:
Model Relationships and "list_display"
(Found via Django Community)
The
list_filter
command is for filtering not ordering. You want theordering
command which is documented here: http://docs.djangoproject.com/en/dev/ref/models/options/#ordering. It's not clear to me though from the documentation if themodeling
Meta command allows ordering through foreign keys. (Thefilter()
queryset function definitely does: http://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by-fields)I've found the admin has some limitations working with deeply hierarchical models. For example, you can inline a child model on its parent's page, but you can't inline an grandchild. Also, by default the
list_filter
command only works on fields in the modeled table. In your example, you couldn't filter on the fields ofPrint
in theOrderInfo
admin for example.