A
A
Alexander2017-09-14 17:55:08
Django
Alexander, 2017-09-14 17:55:08

How to display article archive?

admin.py:

class ArticleAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'author', 'status', 'sub_category', 'show_on_main')
    list_editable = ('status', 'show_on_main')
    inlines = (ParagraphInline,)
    list_per_page = 20
    actions = ['delete_selected']

    def get_queryset(self, request):
        if not request.user.is_superuser and not request.user.groups.filter(name='Moderation'):
            return Article.objects.filter(author=request.user, archive=False)
        return Article.objects.filter(archive=False)

    def delete_model(self, request, obj):
        obj.archive = True
        obj.save()

    def delete_selected(self, request, obj):
        for item in obj:
            item.archive = True
            item.save()

Before deleting the article, I set archive = True for it, then I create a new ArchiveAdmin :
class ArchiveAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'author', 'status', 'sub_category', 'show_on_main')
    inlines = (ParagraphInline,)
    list_per_page = 20

    def get_queryset(self, request):
        return Article.objects.filter(archive=True)

At the end I posted:
admin.site.register(Article, ArticleAdmin)
admin.site.register(Article, ArchiveAdmin)

But Django complains:
django.contrib.admin.sites.AlreadyRegistered: The model Article is already registered

Does anyone have any ideas how this can be implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Kuzmichev, 2017-09-14
@kentuck1213

Unfortunately, more than 1 admin class cannot be registered for the same model.
There is a variant with a proxy model, look here . There is even a similar topic - publications.

A
Alexander Vorobyov, 2018-04-21
@alex_vv

Just in case, there is a second option.
If you set up two admin sites in Django, for example, one for users with a limited set of models, and the second for the admin with all models and / or with archive records.
It can be seen from the question that the architecture of the project just involves separating work with models (why are the archived articles taken out separately for some reason?)
Then you can not create proxies for each "archived" model (if there are a lot of them), but it is easy to register the same model in different admins:

# для всех 
admin.site.register(Article, ArticleAdmin)
# для админа и модераторов архива
archive.register(Article, ArchiveAdmin)

In the example:
A separate admin panel under a separate `url` is written in 2-3 lines in `urls.py` at the root of the project, there is documentation.
Proxies are easy too; one of the pros - you can add separate object managers to the proxy and play with access rights and user permissions in addition to the fact that the model is separately placed in the admin panel.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question