0

我有代码:

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')

这给了我这个: 但是,问题是格式非常难看。我怎样才能得到它:

4

1 回答 1

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,)
于 2013-08-07T17:17:54.310 回答