Search code examples
djangodjango-modelsdjango-formsdjango-adminmanytomanyfield

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


I am using Django 1.7, python 3.4 one of my models contains one class named Enterprise along with five other classes(Type, Products, etc.) each of which have a ManyToMany relation with 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

I am trying to use admin form to populate these fields. After configuring admin for all the five classes, I am defining the EnetrpriseAdmin class like this.

   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)

But it is generating error:

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

I am basically trying to generate a form for populating the Enterprise table which would require populating all five categories with existing database values within those five fields.


Solution

  • The answer suggested by @GwynBleidD is showing error that list_display can not have manytomany field. however I got a workaround by removing those five fields from the list. Now it's working.