定制Django管理界面

定制Django管理界面

Django admin默认是高度响应的GUI,它提供了各种功能和整体的CRUD应用来帮助开发者和用户。此外,Django管理员可以根据自己的需要进行定制,比如在表格的首页显示字段等。在这篇文章中,我们将讨论如何增强Django-admin界面。

项目结构看起来像。
定制Django管理界面

让我们创建一个名为state的应用程序,它有一个同名的模型(state)。当我们在admin.py中注册应用程序时,它显示如下。

state/model.py

from django.db import models
from django.utils import timezone
  
class State(models.Model):
    name = models.CharField(max_length = 50)
    is_active = models.IntegerField(default = 1,
                                   blank = True,
                                    null = True,
                                    help_text ='1->Active, 0->Inactive', 
                                    choices =(
                                    (1, 'Active'), (0, 'Inactive')
                                    ))
    created_on = models.DateTimeField(default = timezone.now)
    updated_on = models.DateTimeField(default = timezone.now,
                                    null = True, 
                                    blank = True
                                    )
    def __str__(self):
        return self.name
  
    class Meta:
        db_table = 'state'

state/admin.py:

from django.contrib import admin
from .models import State
  
admin.site.register(State)

在django管理界面中检查它
定制Django管理界面
现在让我们根据可用的选项来定制django admin。

定制Django管理界面

1.改变模型名称:
如果你想改变这里的模型名称,请打开model.py文件,在meta部分添加verbose_name属性。

state/model.py

from django.db import models
from django.utils import timezone
  
class State(models.Model):
    name = models.CharField(max_length = 50)
    is_active = models.IntegerField(default = 1,
                                   blank = True,
                                    null = True,
                                    help_text ='1->Active, 0->Inactive', 
                                    choices =(
                                    (1, 'Active'), (0, 'Inactive')
                                    ))
    created_on = models.DateTimeField(default = timezone.now)
    updated_on = models.DateTimeField(default = timezone.now,
                                    null = True, 
                                    blank = True
                                    )
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'state'
        # Add verbose name
        verbose_name = 'State List'

输出 :
定制Django管理界面

2.默认情况下,django管理员在列表中只显示对象名称。

定制Django管理界面

人们可以从模型中显示多个字段的数据。在你的admin.py文件中添加几行代码。

state/admin.py:

from django.contrib import admin
from .models import State
  
class StateAdmin(admin.ModelAdmin):
    list_display = ('name', 'active', 'created_on')
  
    def active(self, obj):
        return obj.is_active == 1
  
    active.boolean = True
  
admin.site.register(State, StateAdmin)

输出 :
定制Django管理界面

3.默认情况下,只有一个选项,即删除选项
人们可以在行动下拉菜单上添加更多的选项。

state/admin.py:

from django.contrib import admin
from django.contrib import messages
from .models import State
  
class StateAdmin(admin.ModelAdmin):
    list_display = ('name', 'active', 'created_on')
  
    def active(self, obj):
        return obj.is_active == 1
  
    active.boolean = True
  
    def make_active(modeladmin, request, queryset):
        queryset.update(is_active = 1)
        messages.success(request, "Selected Record(s) Marked as Active Successfully !!")
  
    def make_inactive(modeladmin, request, queryset):
        queryset.update(is_active = 0)
        messages.success(request, "Selected Record(s) Marked as Inactive Successfully !!")
  
    admin.site.add_action(make_active, "Make Active")
    admin.site.add_action(make_inactive, "Make Inactive")
  
admin.site.register(State, StateAdmin)

输出:
定制Django管理界面

4.禁用删除选项:

state/admin.py:

from django.contrib import admin
from django.contrib import messages
from .models import State
  
class StateAdmin(admin.ModelAdmin):
    list_display = ('name', 'active', 'created_on')
  
    def active(self, obj):
        return obj.is_active == 1
  
    active.boolean = True
  
    def make_active(modeladmin, request, queryset):
        queryset.update(is_active = 1)
        messages.success(request, "Selected Record(s) Marked as Active Successfully !!")
  
    def make_inactive(modeladmin, request, queryset):
        queryset.update(is_active = 0)
        messages.success(request, "Selected Record(s) Marked as Inactive Successfully !!")
  
    admin.site.add_action(make_active, "Make Active")
    admin.site.add_action(make_inactive, "Make Inactive")
  
    def has_delete_permission(self, request, obj = None):
        return False
  
admin.site.register(State, StateAdmin)

输出:
定制Django管理界面

5.禁用添加选项:

state/admin.py:

from django.contrib import admin
from .models import State
  
class StateAdmin(admin.ModelAdmin):
    list_display = ('name', 'active', 'created_on')
  
    def active(self, obj):
        return obj.is_active == 1
  
    active.boolean = True
  
    def has_add_permission(self, request):
        return False
  
admin.site.register(State, StateAdmin)

定制Django管理界面

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程