How to register a model twice in the Django admin?
Register a model twice in the Django admin
Play this article
Note: If you want to read the blog series on Django Admin customization, then click here.
Suppose you have a Book model as given below.
# app_name/models.py
class Book(models.Model):
...
title = models.CharField(max_length=150)
is_active = models.BooleanField(default=True)
You might have tried registering a model twice by writing the same code twice, but it gave an error, there is a workaround to register a model
twice, you just have to create a proxy
model of the same model that you want to register, in this example we are going to create a proxy model of Book and then re-registered it.
# app_name/admin.py
from django.contrib import admin
from django.db import models
from app_name.models import Book
class BookProxy(Book):
class Meta:
proxy = True
@admin.register(Book) # First registration of a Book model in Django admin
class BookAdmin(admin.ModelAdmin):
verbose_name = "Books"
@admin.register(BookProxy) # Second registration of a Book model in Django admin
class BookProxyAdmin(admin.ModelAdmin):
verbose_name = "New Books"
Any thoughts? Write it down in the comments.
For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.