1、使用前 2、使用后 3、实现代码
- django admin 后台默认的样式可能不满足我们的需求,这个时候,我们就需要重写默认的样式。
- 我找到的有两种方法,其中一种(formfield_overrides)只能针对某种字段类型,不能指定字段。这里只介绍另一种,可以指定具体哪个字段。
- 这里只实现了提示信息,当然还可以设置输入框长度、宽度等。
- admin.py
@admin.register(models.WarnUser) class WarnUserAdmin(admin.ModelAdmin): """趋势预警用户""" form = forms.WarnUserAdminForm list_display = ('id', 'nickname', 'is_enable', 'duty', 'phone', 'wts', 'remarks') list_editable = ('is_enable', 'duty', 'nickname', 'phone', 'remarks') fields = ('id', 'is_enable', 'duty', 'nickname', 'phone', 'type', 'remarks') ordering = ('id',) empty_value_display = '-auto-' search_fields = ['nickname'] readonly_fields = ['id'] list_per_page = 20 list_display_links = ('id', 'wts') ack = False def get_queryset(self, request): utils.sync_add_ws() return super(WarnUserAdmin, self).get_queryset(request) def save_related(self, request, form, formsets, change): if self.ack: super(WarnUserAdmin, self).save_related(request, form, formsets, change) def save_model(self, request, obj, form, change): sn_user = models.WarnUser.objects.filter(nickname=obj.nickname) if obj.id is None: if not re.match(r"^1[3-9]d{9}$", obj.phone): self.ack = False msg = '【手机号】有误' err_msg(request, msg) elif len(sn_user) != 0: self.ack = False msg = '该昵称已存在' err_msg(request, msg) else: super(WarnUserAdmin, self).save_model(request, obj, form, change) self.ack = True else: super(WarnUserAdmin, self).save_model(request, obj, form, change) self.ack = True def delete_queryset(self, request, queryset): os = queryset.values() for obj in os: wid = obj['id'] models.DataWarnUser.objects.filter(id=wid).delete() super(WarnUserAdmin, self).delete_queryset(request, queryset) def delete_view(self, request, object_id, extra_context=None): models.DataWarnUser.objects.filter(id=object_id).delete() super(WarnUserAdmin, self).delete_view(request, object_id, extra_context)
- forms.py
from django import forms from django.forms import widgets from pms import models class WarnUserAdminForm(forms.ModelForm): class meta: model = models.WarnUser fields = '__all__' widgets = { "nickname": widgets.TextInput(attrs={"style": "width:50%;", "placeholder": "请输入呢称,不可重复"}), }4、另一种方法也贴在下面了,有兴趣的可以看看
@admin.register(models.WarnUser) class WarnUserAdmin(admin.ModelAdmin): """趋势预警用户""" list_display = ('id', 'nickname', 'is_enable', 'duty', 'phone', 'wts', 'remarks') list_editable = ('is_enable', 'duty', 'nickname', 'phone', 'remarks') fields = ('id', 'is_enable', 'duty', 'nickname', 'phone', 'type', 'remarks') ordering = ('id',) empty_value_display = '-auto-' search_fields = ['nickname'] readonly_fields = ['id'] list_per_page = 20 list_display_links = ('id', 'wts') # 重写字段类型的 widget, attrs 属性可以设置前端样式 formfield_overrides = { models.IntegerField: {'widget': widgets.NumberInput(attrs={"style": "width:50em;", })}, models.CharField: {'widget': widgets.TextInput(attrs={"style": "width:50%;", "placeholder": "请输入内容"})}, } ack = False def get_queryset(self, request): utils.sync_add_ws() return super(WarnUserAdmin, self).get_queryset(request) def save_related(self, request, form, formsets, change): if self.ack: super(WarnUserAdmin, self).save_related(request, form, formsets, change) def save_model(self, request, obj, form, change): sn_user = models.WarnUser.objects.filter(nickname=obj.nickname) if obj.id is None: if not re.match(r"^1[3-9]d{9}$", obj.phone): self.ack = False msg = '【手机号】有误' err_msg(request, msg) elif len(sn_user) != 0: self.ack = False msg = '该昵称已存在' err_msg(request, msg) else: super(WarnUserAdmin, self).save_model(request, obj, form, change) self.ack = True else: super(WarnUserAdmin, self).save_model(request, obj, form, change) self.ack = True def delete_queryset(self, request, queryset): os = queryset.values() for obj in os: wid = obj['id'] models.DataWarnUser.objects.filter(id=wid).delete() super(WarnUserAdmin, self).delete_queryset(request, queryset) def delete_view(self, request, object_id, extra_context=None): models.DataWarnUser.objects.filter(id=object_id).delete() super(WarnUserAdmin, self).delete_view(request, object_id, extra_context)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)