Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting the error <class 'blog_app.admin.RequestDemoAdmin'>: (admin.E109) The value of 'list_display[6]' must not be a ManyToManyField

I have been getting this error when trying to migrate my models. However, i am getting this error <class ‘blog_app.admin.RequestDemoAdmin’>: (admin.E109) The value of ‘list_display[6]’ must not be a ManyToManyField. Any help would be much appreciated.

models.py

from django.db import models
from taggit.managers import TaggableManager



# Create your models here.
class IpModel(models.Model):
    ip = models.CharField(max_length=100)

    def __str__(self):
        return self.ip

class Blog_Post(models.Model):
    slug = models.SlugField(max_length=1000, editable=False, null=True)
    post_title = models.CharField(max_length=100, editable=True, blank=False, null=True)
    blog_content = models.TextField(max_length=10000, blank=False, editable=True, null=True)
    files = models.FileField(blank=True, null=True, upload_to=True)
    date = models.DateTimeField(blank=False, null=True, auto_now=True, editable=False)
    likes = models.ManyToManyField(IpModel, related_name="post_likes", blank=True)

    def save(self, *args, **kwargs):
        self.slug = self.slug
        super().save(*args, **kwargs)

    def total_likes(self):
        return self.likes.count()

here is the admin.py file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    from django.contrib import admin
from .models import Blog_Post

@admin.register(Blog_Post)
class RequestDemoAdmin(admin.ModelAdmin):
  list_display = [field.name for field in
  Blog_Post._meta.get_fields()]

>Solution :

You can’t include a ManyToManyField in list_display, you include all field names from Blog_Post._meta.get_fields() including a ManyToManyField.

Filter out ManyToManyFields by using Field.many_to_many:

@admin.register(Blog_Post)
class RequestDemoAdmin(admin.ModelAdmin):
    list_display = [field.name for field in Blog_Post._meta.get_fields() if not field.many_to_many]

Alternatively, explicitly define the fields in list_display

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading