[Fixed]-Django – two sections from the same model

1👍

You can achieve this by registering an admin page for a proxy model, and overriding the get_queryset method of the registered ModelAdmin.

You can only register an object once in the admin site, so the proxy model acts as a new model to be registered separately. Since it’s only a proxy model, its presence only impacts Python code and won’t create a new model in the database.

The get_queryset command is how ModelAdmin gets the model instances to display, so by overriding it we can add whatever filtering we want.

In your case you would add something like this to the admin.py file in your app.

class InactiveSite(Site):
    class Meta:
        proxy = True
        verbose_name_plural = 'Inactive sites'

class InactiveSiteAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        return self.model.objects.filter(is_active=False)

admin.site.register(InactiveSite, InactiveSiteAdmin)

Leave a comment