How to customize a many-to-many inline model in django admin

Posted by Jonathan on Stack Overflow See other posts from Stack Overflow or by Jonathan
Published on 2010-05-23T23:25:04Z Indexed on 2010/05/23 23:31 UTC
Read the original article Hit count: 525

I'm using the admin interface to view invoices and products. To make things easy, I've set the products as inline to invoices, so I will see the related products in the invoice's form. As you can see I'm using a many-to-many relationship.

In models.py:

class Product(models.Model):
    name  = models.TextField()
    price = models.DecimalField(max_digits=10,decimal_places=2)

class Invoice(models.Model):
    company  = models.ForeignKey(Company)
    customer = models.ForeignKey(Customer)
    products = models.ManyToManyField(Product)

In admin.py:

class ProductInline(admin.StackedInline):
    model = Invoice.products.through

class InvoiceAdmin(admin.ModelAdmin):
    inlines = [FilteredApartmentInline,]
admin.site.register(Product, ProductAdmin)

The problem is that django presents the products as a table of drop down menus (one per associated product). Each drop down contains all the products listed. So if I have 5000 products and 300 are associated with a certain invoice, django actually loads 300x5000 product names. Also the table is not aesthetic.

How can I change it so that it'll just display the product's name in the inline table? Which form should I override, and how?

© Stack Overflow or respective owner

Related posts about python

Related posts about django