有三张表,其中表customerGroup跟表discount是一对一关系,跟表product是多对多关系。
models.py
class customerGroup(models.Model):
name = models.CharField(max_length=100)
discount = models.ForeignKey("discount")
products = models.ManyToManyField("product",blank=True)
def __unicode__(self):
return self.name
class product(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class discount(models.Model):
name=models.CharField(max_length=30)
discountValue = models.DecimalField(max_digits=3,decimal_places=2)
def __unicode__(self):
return self.name
定义list_display的显示内容:
models.py中添加如下代码:
from django.contrib import admin
class customerGroupAdmin(admin.ModelAdmin):
list_display=('name','discountValue','product_list','comment')
filter_horizontal = ('products',)
def discountValue(self,obj):
return obj.discount.discountValue
def product_list(self,obj):
products=""
for product in obj.products.all():
products=product.name+","+products
return products
admin.site.register(customerGroup,customerGroupAdmin)
参考文档:https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display