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
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()]
CodePudding user response:
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