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

Using the tables defined in the same models.py for list_display and populating the manyToMany fields of that table in admin forms

我正在使用 Django 1.7,python 3.4 我的一个模型包含一个名为 Enterprise 的 class 以及其他五个 classes(类型、产品等),每个模型都与 Enterprise 具有多对多关系。

    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

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

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

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

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

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

@GwynBleidD 建议的答案显示 list_display 不能有 manytomany 字段的错误。 但是我通过从列表中删除这五个字段得到了一个解决方法。现在可以使用了。