我有代码:
class PurchaseOrder(models.Model):
product = models.ManyToManyField('Product', null =True)
def get_products(self):
return "\n".join([p.products for p in self.product.all()])
class Product(models.Model):
products = models.CharField(max_length=256, null =True)
def __unicode__(self):
return self.products
在views.py中:
class PurchaseOrderAdmin(admin.ModelAdmin):
fields = ['product']
list_display = ('get_products')
这给了我这个: 但是,问题是格式非常难看。我怎么能这样得到它:
答案 0 :(得分:1)
您可以使用filter_horizontal
:
class PurchaseOrderAdmin(admin.ModelAdmin):
list_display = ('get_products')
filter_vertical = ['product']
或者您可以使用inline
:
class ProductInline(admin.TabularInline):
model = Product
class PurchaseOrderAdmin(admin.ModelAdmin):
inlines = (ProductInline,)