Django Xadmin多對(duì)多字段過濾實(shí)例
在xadmin中是不能像原生admin那樣使用formfield_for_manytomany方法來過濾多對(duì)多字段
進(jìn)入xadmin源碼,找到了formfield_for_dbfield這個(gè)方法,測(cè)試是有用的,可以過濾第一個(gè)選項(xiàng)框的值
補(bǔ)充知識(shí):給django admin后臺(tái)管理user擴(kuò)展下拉框及多選框的字段
1.首先在models.py中編寫擴(kuò)展User所用到的userProfile模型及下拉框和多選框選項(xiàng)值所需要的模型(因?yàn)槲宜龅南吕蚝投噙x框的值都是從數(shù)據(jù)庫里面取得),代碼如下:
2.第二步編寫admin.py對(duì)User字段進(jìn)行擴(kuò)展,代碼如下:
# -*- coding: UTF-8 -*-from django.contrib import adminfrom django import formsfrom TESTAPP.models import test,userProfilefrom django.contrib.auth.admin import UserAdminfrom django.contrib.auth.models import User # Register your models here.class userProfileForm(forms.ModelForm): option = forms.ModelChoiceField(label=u’下拉框’,queryset=test.objects.all()) checkbox = forms.ModelMultipleChoiceField(label=u’多選框’,queryset=test.objects.all(),widget=forms.CheckboxSelectMultiple()) class Meta: model = userProfile fields = [’option’,’checkbox’]class profileInline(admin.StackedInline): model = userProfile form = userProfileFormclass testUserAdmin(UserAdmin): inlines = [profileInline,]admin.site.unregister(User)admin.site.register(User, testUserAdmin)
通過這兩步就可以試下在django admi臺(tái)管理User中擴(kuò)展一個(gè)下拉框和一個(gè)多選框,效果如下圖:
以上這篇Django Xadmin多對(duì)多字段過濾實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁2. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法3. asp知識(shí)整理筆記4(問答模式)4. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?5. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼6. ASP實(shí)現(xiàn)加法驗(yàn)證碼7. XML解析錯(cuò)誤:未組織好 的解決辦法8. 小技巧處理div內(nèi)容溢出9. js的一些潛在規(guī)則使用分析10. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)
