Register same model twice on admin - is there a way?

Let’s imagine the following model:

class Photo(models.Model):
    class Type(models.IntegerChoices):
        LANDSCAPE = 1, 'Landscape'
        CLOSEUP = 2, 'Close-up'
        # etc...

    type = models.PositiveSmallIntegerField('Type', blank=False, null=False, choices=Type.choices, default=Type.LANDSCAPE, db_column='t')

And in the corresponding admin.py:

class PhotoLandscapeAdmin(admin.ModelAdmin):
        def get_queryset(self, request):
            return super(PhotoLandscapeAdmin, self).get_queryset(request).filter(
                type=Photo.Type.LANDSCAPE
            )

class PhotoCloseupAdmin(admin.ModelAdmin):
        def get_queryset(self, request):
            return super(PhotoCloseupAdmin, self).get_queryset(request).filter(
                type=Photo.Type.CLOSEUP
            )

admin.site.register(Photo, PhotoLandscapeAdmin)
admin.site.register(Photo, PhotoCloseupAdmin)

and I get:

django.contrib.admin.sites.AlreadyRegistered: The model Photo is already registered with 'photo.PhotoLandscapeAdmin'.

Is there a way to do this? i.e, to have two “entries” in the admin panel/menu?

I know something very similar can be done via filters and what not, but having those two “shortcuts” would be very useful.

Thanks! :slight_smile:

Yes, it can be done with a proxy model.

See Models | Django documentation | Django and the related thread about the admin at Multiple ModelAdmins/views for same model in Django admin

2 Likes

Sure do!
But you need to create a ProxyModel for that.
You can do that by adding these to your models.py

# Create a class with another name, that inherits from the Photo model
# It must have proxy = True on the Meta class
class OtherTypePhoto(Photo):
    class Meta:
        proxy = True

Then register the proxied model on the admin.py

admin.site.register(OtherTypePhoto, PhotoCloseupAdmin)

1 Like

nice, thanks :slight_smile:
[20 characters]