开发者

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

开发者 https://www.devze.com 2022-12-31 23:41 出处:网络
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 s

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

0

精彩评论

暂无评论...
验证码 换一张
取 消