使用在相同models.py中为list_display定义的表,并在管理表单中填充该表的manyToMany字段

时间:2015-02-21 17:45:05

标签: django django-models django-forms django-admin manytomanyfield

我正在使用Django 1.7,python 3.4 我的一个模型包含一个名为Enterprise的类以及另外五个类(Type,Products等),每个类与Enterprise具有ManyToMany关系。

    class Enterprise(models.Model):
        enterprise = models.CharField(max_length=255)
        slug = models.SlugField(max_length=255, unique=True)
        is_active = models.BooleanField(default=True)
        types = models.ManyToManyField(Type)
        products = models.ManyToManyField(Product)
        assets = models.ManyToManyField(Asset)
        operations = models.ManyToManyField(Operation)
        materials = models.ManyToManyField(Material)

        def __str__(self):
            return self.enterprise

我正在尝试使用管理表单来填充这些字段。在为所有五个类配置admin之后,我就像这样定义了EnetrpriseAdmin类。

   class EnterpriseAdmin(admin.ModelAdmin):
       list_display = ['enterprise', 'slug', 'Type', 'Operation', 'Asset',
                       'Material', 'Products']
       fieldsets = (
           (None, {'fields': ('Enterprise', 'slug')}),
           ('Categorization', {'fields': ('enterprise', 'slug', 'Type', 'Operation', 'Asset', 'Material', 'Products')}),                                            
       )

       add_fieldsets = (
                  (None, {'classes': ('wide',),
               'fields': ('enterprise', 'slug', 'Type', 'Operation','Asset',
                          'Material', 'Products')}
                  ),
       )
       search_fields = ('enterprise',)
       ordering = ('enterprise',)

   admin.site.register(Enterprise, EnterpriseAdmin)

但它产生错误:

    <class 'enterprise.admin.EnterpriseAdmin'>: (admin.E108) The value of         'list_disp
    lay[2]' refers to 'Type', which is not a callable, an attribute of 'EnterpriseAd
    min', or an attribute or method on 'enterprise.Enterprise'.
    <class 'enterprise.admin.EnterpriseAdmin'>: (admin.E108) The value of 'list_disp
    lay[3]' refers to 'Operation', which is not a callable, an attribute of 'Enterpr
    iseAdmin', or an attribute or method on 'enterprise.Enterprise'.
    <class 'enterprise.admin.EnterpriseAdmin'>: (admin.E108) The value of 'list_disp
    lay[4]' refers to 'Asset', which is not a callable, an attribute of 'EnterpriseA
    dmin', or an attribute or method on 'enterprise.Enterprise'.
    <class 'enterprise.admin.EnterpriseAdmin'>: (admin.E108) The value of 'list_disp
    lay[5]' refers to 'Material', which is not a callable, an attribute of 'Enterpri
    seAdmin', or an attribute or method on 'enterprise.Enterprise'.
    <class 'enterprise.admin.EnterpriseAdmin'>: (admin.E108) The value of 'list_disp
    lay[6]' refers to 'Products', which is not a callable, an attribute of 'Enterpri
    seAdmin', or an attribute or method on 'enterprise.Enterprise'.

我基本上是在尝试生成一个用于填充Enterprise表的表单,该表需要使用这五个字段中的现有数据库值填充所有五个类别。

2 个答案:

答案 0 :(得分:0)

不使用相关模型类的名称,而是使用关系字段名称,例如:

    list_display = ['enterprise', 'slug', 'types', 'perations', 'assets',
                   'materials', 'products']

答案 1 :(得分:0)

@GwynBleidD建议的答案显示list_display不能有多个字段的错误。 但是我通过从列表中删除这五个字段得到了解决方法。现在它正在发挥作用。