How to configure the built-in many-to-many model in django admin

I use the admin interface to view invoices and products. To simplify the task, I installed the products as embedded in the invoices, so I will see the related products in the form of an invoice. As you can see, I use many-to-many relationships.

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 dropdown menu table (one per related product). Each drop-down list contains all of the products listed. Therefore, if I have 5,000 products and 300 associated with a specific account, django actually loads 300x5000 product names. Also, the table is not aesthetic.

I do not need products to be updated through the invoice form. How can I change it so that it only displays the product name in the embedded table? What form should I redefine and how?

+6
python django django-admin django-forms many-to-many
source share
1 answer

I think it is simple, do not use the inline string, just use the ModelAdmin.filter_horizontal property

+4
source share

All Articles