收到错误 <class 'blog_app.admin.RequestDemoAdmin'>: (admin.E109) 'list_display[6]' 的值不能是 ManyToManyField

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

我在尝试迁移我的模型时遇到了这个错误。但是,我收到此错误 : (admin.E109) 'list_display[6]' 的值不能是 ManyToManyField。任何帮助将不胜感激。

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()

这里是 admin.py 文件

    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()]

您不能在 list_display 中包含 ManyToManyField,您可以包含 Blog_Post._meta.get_fields() 中的所有字段名称,包括 ManyToManyField.

使用 Field.many_to_many:

过滤掉 ManyToManyFields
@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]

或者,显式定义 list_display

中的字段