自定义 Django 管理界面
默认情况下,Django admin 是高度响应的 GUI,它提供各种功能和整体 CRUD 应用程序来帮助开发人员和用户。此外,Django admin 可以自定义以满足个人需求,例如在表格的主页上显示字段等。在本文中,我们将讨论如何增强 Django-admin 接口。
项目结构如下:
让我们创建一个名为state的应用程序,它有一个具有相同名称(状态)的模型。当我们将应用程序注册到 admin.py 时,它显示如下。
状态/模型.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'
状态/admin.py:
from django.contrib import admin
from .models import State
admin.site.register(State)
在 django 管理界面中检查它
现在让我们根据可用选项自定义 django admin。
自定义 Django 管理界面
1.更改型号名称:
如果您想更改模型的名称,即此处的状态,请打开 model.py 文件并在元部分添加 verbose_name 属性。
状态/模型.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'
输出 :
2. 默认情况下,django admin 在列表中只显示对象名称。
可以显示模型中的多个字段数据。在 admin.py 文件中添加几行代码。
状态/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)
输出 :
3. 默认情况下只有一个选项,即删除选项。
可以在操作下拉列表中添加更多选项:
状态/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)
输出:
4.禁用删除选项:
状态/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)
输出:
5.删除添加选项:
状态/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)
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。