How to customize a many-to-many inline model in django admin
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开发者_如何学JAVA.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.
I don't need the products to be updatable via the invoice form. 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?
I think is simple, don't use the inline, just use the property ModelAdmin.filter_horizontal
精彩评论