技术 django
Django修改admin后台管理表单

Django修改admin后台管理表单

官方文档有相关说明,不懂得可以去查阅。其实说实话查阅起来还是挺麻烦的,尤其是一些一知半解的知识点。我就以我的使用体验了简单梳理一下,如何定制后端的表单显示以一个最普通的Model为例。

涉及到后端管理表单的自定义和修改,主要有如下两点明确,就可以方便的修改和自定义。

新定义的表单django.forms.ModelForm--做为客户端的控制

管理模块django.admin.ModelAdmin --做为服务端的控制

  1. 在models.py中我有如下的数据库Blog
#coding: utf-8
from django.db import models


# Create your models here.

class Blog(models.Model):
    caption = models.CharField(max_length=200,verbose_name=u"标题")
    content = models.TextField(verbose_name="内容")
    visit = models.IntegerField(verbose_name=u"访客")
    weight = models.IntegerField(verbose_name=u"权重")
    date  = models.DateField(auto_now=True, verbose_name=u"更新时间")
    class Meta:
        verbose_name = u"文章"
    def __unicode__(self):
        return '%s %s'%(self.caption,self.date)

对应的默认后台管理表单

  1. 如果要修改客户端显示,即主要修改forms.ModelForm,主要的几个属性就是widget、CharField等
class BlogForm(forms.ModelForm):
    content = forms.CharField(initial='like oldrss',widget=forms.Textarea(attrs={'class':'vLargeTextField'}))
    image = forms.ImageField()
    visit = forms.CharField(initial=0,widget=forms.TextInput(attrs={'readonly':'true'}))
    uid = forms.CharField(initial=0, widget=forms.TextInput(attrs={'readonly':'true'}))
    class Meta:
        forms.model = Blog

forms的属性可以这样查看

>>> dir(django.forms)
[u'ALL_FIELDS', u'BaseForm', u'BaseModelForm', u'BooleanField', u'CharField', u'
CheckboxInput', u'CheckboxSelectMultiple', u'ChoiceField', u'ClearableFileInput'
, u'ComboField', u'DateField', u'DateInput', u'DateTimeField', u'DateTimeInput',
 u'DecimalField', u'EmailField', u'EmailInput', u'Field', u'FileField', u'FileIn
put', u'FilePathField', u'FloatField', u'Form', u'GenericIPAddressField', u'Hidd
enInput', u'IPAddressField', u'ImageField', u'IntegerField', u'Media', u'MediaDe
finingClass', u'ModelChoiceField', u'ModelForm', u'ModelMultipleChoiceField', u'
MultiValueField', u'MultiWidget', u'MultipleChoiceField', u'MultipleHiddenInput'
, u'NullBooleanField', u'NullBooleanSelect', u'NumberInput', u'PasswordInput', u
'RadioSelect', u'RegexField', u'Select', u'SelectMultiple', u'SlugField', u'Spli
tDateTimeField', u'SplitDateTimeWidget', u'TextInput', u'Textarea', u'TimeField'
, u'TimeInput', u'TypedChoiceField', u'TypedMultipleChoiceField', u'URLField', u
'URLInput', 'ValidationError', u'Widget', '__builtins__', '__doc__', '__file__',
 '__name__', '__package__', '__path__', 'absolute_import', 'fields', u'fields_fo
r_model', 'forms', 'formsets', u'model_to_dict', 'models', u'save_instance', 'ut
il', 'widgets']
  1. 修改表单显示
from django import forms

class BlogForm(forms.ModelForm):
    content = forms.CharField(initial='like oldrss',widget=forms.Textarea(attrs={'class':'vLargeTextField'}))
    image = forms.ImageField()
    visit = forms.CharField(initial=0,widget=forms.TextInput(attrs={'readonly':'true'}))
    uid = forms.CharField(initial=0, widget=forms.TextInput(attrs={'readonly':'true'}))
    class Meta:
        forms.model = Blog

其中initial初始化表单的值,相当于value的值。widget后面的参数attrs是属性设置。

  1. 控制表单的显示
class BlogAdmin(admin.ModelAdmin):
    list_display = ('caption','uid','date')#modify display
    search_fields = ('caption','uid')#add search
    list_filter = ('date',)
    date_hierarchy = 'date'
    form = BlogForm
    fields = ('caption','content','visit','weight')

list_display 是ModelAdmin的一个属性,控制显示以上几个字段,searc_fields 控制显示一个搜索框,list_filter控制date为过滤选项等等。fields控制表单只显示如上的几个属性。

以上即可以实现修改一个表单。一份较为完整的代码如下。

from django.contrib import admin
from models import *
from django import forms

class BlogForm(forms.ModelForm):
    content = forms.CharField(initial='like oldrss',widget=forms.Textarea(attrs={'class':'vLargeTextField'}))
    image = forms.ImageField()
    visit = forms.CharField(initial=0,widget=forms.TextInput(attrs={'readonly':'true'}))
    uid = forms.CharField(initial=0, widget=forms.TextInput(attrs={'readonly':'true'}))
    class Meta:
        forms.model = Blog



class BlogAdmin(admin.ModelAdmin):
    list_display = ('caption','uid','date')#modify display
    search_fields = ('caption','uid')#add search
    list_filter = ('date',)
    date_hierarchy = 'date'
    form = BlogForm
    fields = ('caption','content','visit','weight')


# Register your models here.
admin.site.register(Blog,BlogAdmin)
"文章为作者独立观点, 不代表老订阅立场"

本站作者

每日荐书

在不完美的世界力求正常——读《公司的坏话》

书名:《公司的坏话》

作者:李天田(脱不花妹妹)

出版社:北京大学出版社

赞助商

广告