Django Admin:将多个管理类注册到同一模型 [英] Django Admin: Register multiple admin classes to the same model

查看:33
本文介绍了Django Admin:将多个管理类注册到同一模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将多个管理类注册到同一模型?我想同时将PostAdmin和MyPostAdmin注册到Post模型.现在,我正在尝试将代理模型与MyPost一起使用,但这在管理面板中为我提供了两种不同的模型,它们具有各自的功能.

Is it possible to register multiple admin classes to the same model? I want to have both PostAdmin and MyPostAdmin register to the Post model. Right now I'm trying to use a proxy model with MyPost, but it's giving me two different models in the admin panel with their separate functionality.

admin.py:

class PostAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug':('title',)}

class MyPostAdmin(SummernoteModelAdmin):
    summernote_fields = ('text', )

admin.site.register(Post, PostAdmin)
admin.site.register(MyPost, MyPostAdmin)

models.py:

models.py:

class Post(models.Model):
    title = models.CharField(max_length=250)
    category = models.IntegerField(choices=category_choices, default=0)
    description = models.TextField(max_length=250, blank=True, null=True)
    text = models.TextField()
    thumbnail = models.ImageField(upload_to=settings.MEDIA_URL)
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True, null=True)
    slug = models.SlugField(unique=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

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

    def __str__(self):
        return self.title


class MyPost(Post):

    class Meta:
        proxy = True

推荐答案

原来,我不需要制作代理模型.我通过将prepopulated_fields变量添加到MyPostAdmin类中解决了该问题

Turns out I didn't need to make a proxy model. I fixed the issue by adding the prepopulated_fields variable to the MyPostAdmin class

这篇关于Django Admin:将多个管理类注册到同一模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆