标签:
三张表:应用名称表"appname",单位表"unit",应用名称表和单位表多对多关系表"unit_unit_permissionList"
model_appname.py
# -*- coding: utf-8 -*- from django.contrib import admin from django.db import models from django.utils.translation import ugettext_lazy as _ __author__ = ‘Administrator‘ APPLICATION_CATEGORY = (("notice", _(u"公告")), ("report", _(u"报表")), ("maintain", _(u"维护记录")), ("investigation", _(u"驻村检查")), ("fire_drill", _(u"消防演练")), ("fire_alarm", _(u"火灾119")), ("inspection", _(u"鸣锣喊寨")), ) class AppName(models.Model): """应用名称""" id = models.AutoField(primary_key=True, editable=False) name = models.CharField(_(u"应用名称"), choices=APPLICATION_CATEGORY, max_length=100, default="", unique=True) create_time = models.DateTimeField(_(u"创建时间"), db_index=True, auto_now_add=True) class Meta: verbose_name = _(u"应用名称") get_latest_by = ["create_time"] verbose_name_plural = verbose_name db_table = "appname" def __unicode__(self): return self.name class AppNameAdmin(admin.ModelAdmin): list_display = ["id", "name", "create_time"]
model_unit.py
# -*- coding: utf-8 -*- from django.contrib import admin from django.db import models from django.utils.translation import ugettext_lazy as _ import uuid from model_appname import AppName class Unit(models.Model): """ 社会单位 """ id = models.AutoField(primary_key=True, editable=False) name = models.CharField(_(u"单位名称"), max_length=100, unique=True, db_index=True) address = models.CharField(_(u"单位地址"), max_length=100, null=True, blank=True, db_index=True) permissionList = models.ManyToManyField(AppName) class Meta: verbose_name = _(u"单位信息") verbose_name_plural = verbose_name db_table = ‘unit‘
form_unit.py
# -*- coding: utf-8 -*- from django import forms from django.forms import ModelForm from django.utils.translation import ugettext_lazy as _ from mysite.manage.models.model_unit import Unit from mysite.manage.models.model_appname import AppName class UnitForm(ModelForm): permissionList = forms.MultipleChoiceField(label=u‘权限列表‘, widget=forms.CheckboxSelectMultiple()) def __init__(self, *args, **kwargs): """初始化permissionList权限列表的值""" super(UnitForm, self).__init__(*args, **kwargs) self.fields[‘permissionList‘].choices = ((x.id, x.get_name_display()) for x in AppName.objects.all()) class Meta: model = Unit fields = [‘name‘, ‘address‘] widgets = { ‘address‘: forms.TextInput(attrs={‘class‘: ‘form-control‘}), }
admin.py
from django.contrib import admin from models.model_unit import Unit from form.form_unit import UnitForm from models.model_appname import AppName, AppNameAdmin class UnitAdmin(admin.ModelAdmin): list_display = [‘id‘, ‘name‘, ‘address‘] search_fields = (‘name‘, ‘contactnumber‘) form = UnitForm admin.site.register(Unit, UnitAdmin) admin.site.register(AppName, AppNameAdmin)
标签:
原文地址:http://www.cnblogs.com/wangmin0216/p/5594311.html