Vaibhav Jain
Vaibhav Jain

Reputation: 5507

how to display many error django

i have following models

class tags(models.Model):
    tag = models.CharField(max_length=15) # Tag name
    tagDescription = models.TextField()   # Tag Description
    tagSlug = models.TextField()          # Extra info can be added to the existing tag using this field

class stores(models.Model):
    storeName = models.CharField(max_length=15)                                          # Store Name
    storeDescription = models.TextField()                                                # Store Description
    storeURL = models.URLField()                                                         # Store URL
    storePopularityNumber = models.IntegerField(max_length=1)                            # Store Popularity Number
    storeImage = models.ImageField(upload_to=storeImageDir)                              # Store Image 
    storeSlug = models.TextField()                                                       # This is the text you see in the URL
    createdAt = models.DateTimeField()                                                   # Time at which store is created
    updatedAt = models.DateTimeField()                                                   # Time at which store is updated
    storeTags = models.ManyToManyField(tags)

class tagsAdmin(admin.ModelAdmin):
    list_display = ('tag', 'tagDescription', 'tagSlug')

class storesAdmin(admin.ModelAdmin):
    list_display = ('storeName','storeDescription','storeURL',
                    'storePopularityNumber','storeImage',
                    'storeSlug','createdAt','createdAt','storeTags'
                    )

admin.site.register(tags,tagsAdmin)
admin.site.register(stores,storesAdmin)

Whenever I am trying to run command : python manage.py syncdb I got the error: django.core.exceptions.ImproperlyConfigured: 'storesAdmin.list_display[8]', 'storeTags' is a ManyToManyField which is not supported.

I don't understand what I am doing wrong here. I want to simply display all the models in the django admin site.

Upvotes: 0

Views: 81

Answers (2)

Henrik Andersson
Henrik Andersson

Reputation: 47172

You can't reference a Many2ManyField like that, you have to use a method instead in the stores class that looks like this

def get_tags(): 
    return self.storeTags.all()

and reference that in your list_display(...'get_tags')

This is done because the M2M field would result in lots of SQL queries that would slow the entire thing down so therefore the choice would have to come from the developer and not from the framework.

Upvotes: 1

Pancho Jay
Pancho Jay

Reputation: 500

Please check:

ModelAdmin.list_display

"ManyToManyField fields aren’t supported, because that would entail executing a separate SQL statement for each row in the table. If you want to do this nonetheless, give your model a custom method, and add that method’s name to list_display. (See below for more on custom methods in list_display.)"

You can use a custom method to show values of ManyToManyField or simply remove storeTags from list_display

Upvotes: 0

Related Questions