码迷,mamicode.com
首页 > 其他好文 > 详细

django form表单动态加载下拉列表选项内容

时间:2016-06-17 17:06:56      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

三张表:应用名称表"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)

技术分享

 

django form表单动态加载下拉列表选项内容

标签:

原文地址:http://www.cnblogs.com/wangmin0216/p/5594311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!