Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin: Register multiple admin classes to the same model

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:

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
like image 347
Adiosz Avatar asked Sep 04 '26 21:09

Adiosz


1 Answers

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

like image 161
Adiosz Avatar answered Sep 07 '26 11:09

Adiosz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!