banner
raye~

Raye's Journey

且趁闲身未老,尽放我、些子疏狂。
medium
tg_channel
telegram
twitter
douban
github
email
nintendo switch
playstation
steam_profiles
zhihu

[Django Backend Development Part 3] Backend Management Implementation

【Django Backend Development 1】Environment Deployment and Project Initialization

【Django Backend Development 2】Model Design and Creation

In this section, we will talk about how to reuse the Django backend system to implement user management.

First, we need to change the backend system to Chinese and modify the name to our own (pretending to be self-developed).

Modify in ./easy_talk_backend/study/admin.py:

admin.site.site_header = 'xxx Backend System'

Also, modify the language to Chinese in the project's ./easy_talk_backend/settings.py:

LANGUAGE_CODE = 'zh-Hans'

Then, let's implement the management of study content. Import the models we created in the previous section in admin.py:

from .models import StudyContent, PushStrategy, SchoolClass, Student, Checkin

Register to the backend:

@admin.register(StudyContent)
class StudyContentAdmin(admin.ModelAdmin):
    pass

Now the backend can manage this StudyContent, but it is too simple. We need to customize it.

Decide the properties to display: list_display
Properties that support search: search_fields
Filter fields: list_filter
Disable the original delete and add new online operations: actions

The first three are quite simple, the code is as follows:

@admin.register(StudyContent)
class StudyContentAdmin(admin.ModelAdmin):
    list_display = ('title', 'brief', 'url', 'is_online')
    search_fields = ('title', 'url')
    actions = ['mark_as_online']
    list_filter = ('is_online',)
    pass

mark_as_online is our custom operation, which means this operation needs to update the is_online attribute.

    def mark_as_online(self, request, queryset):
        queryset.update(is_online=True)
        self.message_user(request, 'The selected study content has been successfully marked as online')
    mark_as_online.short_description = 'Mark selected study content as online'

Disable the delete operation:

    def get_actions(self, request):
        actions = super().get_actions(request)
        if 'delete_selected' in actions:
            del actions['delete_selected']
        return actions

Finally, it's done~, the code is as follows:

@admin.register(StudyContent)
class StudyContentAdmin(admin.ModelAdmin):
    list_display = ('title', 'brief', 'url', 'is_online')
    search_fields = ('title', 'url')
    actions = ['mark_as_online']
    list_filter = ('is_online',)

    def mark_as_online(self, request, queryset):
        queryset.update(is_online=True)
        self.message_user(request, 'The selected study content has been successfully marked as online')
    mark_as_online.short_description = 'Mark selected study content as online'

    def get_queryset(self, request):
        return super().get_queryset(request)

    def get_actions(self, request):
        actions = super().get_actions(request)
        if 'delete_selected' in actions:
            del actions['delete_selected']
        return actions

Except for user management, the rest can be done in this way.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.