为什么不能使用自定义模型管理器通过list_filter编辑模型?

时间:2019-11-30 01:04:40

标签: django django-models django-admin

对于邮政模型,我创建了一个自定义管理器:

#models.py
class ObjectsOnManager(models.Manager):
    def get_queryset(self):
        return super(ObjectsOnManager, self).get_queryset().filter(status='on')


class OnOffStatusModel(models.Model):
    ON = 'on'
    OFF = 'off'
    STATUS_CHOICES = (
        (ON, 'Показывать'),
        (OFF, 'Не показывать'),
    )
    status = models.CharField("Статус", max_length=15, choices=STATUS_CHOICES, default=ON)
    objects_on = ObjectsOnManager()
    objects = models.Manager()

    class Meta:
        abstract = True

class Post(OnOffStatusModel):
    count = models.PositiveIntegerField("Показы", default=0)
    title = models.CharField("Заголовок", max_length=50)
    descript = models.CharField("Описание", max_length=100)
    slug = models.SlugField(max_length=50, unique=True)
    category = models.ManyToManyField(Category, related_name='posts')
    pub_date = models.DateTimeField("Дата публикации", default=timezone.now, blank=True)
    body = models.TextField("Текст поста", max_length=2500)
    main_post = models.BooleanField("Главная новость", default=False)

然后在modelAdmin中更改了get_queryset

#admin.py
class PostAdmin(admin.ModelAdmin):
    form = PostImageControlForm
    fields = ('count', 'status',  'image',  'title', 'descript', 'body', 'main_post', )
    list_display = ('title', 'main_post', 'count',)
    list_editable = ('main_post', 'status')
    list_filter = ('category', 'main_post', 'status')

    def get_queryset(self, request):
        return Post.objects.all()

因此,如果我在变更模型页面上编辑模型是可以的,但是如果我尝试在变更列表页面上进行编辑,则会出现错误 enter image description here

0 个答案:

没有答案