Formatting inline many-to-many related models presented in django admin

Posted by Jonathan on Stack Overflow See other posts from Stack Overflow or by Jonathan
Published on 2010-05-25T20:46:55Z Indexed on 2010/05/25 20:51 UTC
Read the original article Hit count: 392

I've got two django models (simplified):

class Product(models.Model):
    name  = models.TextField()
    price = models.IntegerField()

class Invoice(models.Model):
    company  = models.TextField()
    customer = models.TextField()
    products = models.ManyToManyField(Product)

I would like to see the relevant products as a nice table (of product fields) in an Invoice page in admin and be able to link to the individual respective Product pages.

My first thought was using the admin's inline - but django used a select box widget per related Product. This isn't linked to the Product pages, and also as I have thousands of products, and each select box independently downloads all the product names, it quickly becomes unreasonably slow.

So I turned to using ModelAdmin.filter_horizontal as suggested here, which used a single instance of a different widget, where you have a list of all Products and another list of related Products and you can add\remove products in the later from the former. This solved the slowness, but it still doesn't show the relevant Product fields, and it ain't linkable.

So, what should I do? tweak views? override ModelForms? I Googled around and couldn't find any example of such code...

© Stack Overflow or respective owner

Related posts about python

Related posts about django