Django Xadmin多對多字段過濾實(shí)例
在xadmin中是不能像原生admin那樣使用formfield_for_manytomany方法來過濾多對多字段
進(jìn)入xadmin源碼,找到了formfield_for_dbfield這個方法,測試是有用的,可以過濾第一個選項(xiàng)框的值
補(bǔ)充知識:給django admin后臺管理user擴(kuò)展下拉框及多選框的字段
1.首先在models.py中編寫擴(kuò)展User所用到的userProfile模型及下拉框和多選框選項(xiàng)值所需要的模型(因?yàn)槲宜龅南吕蚝投噙x框的值都是從數(shù)據(jù)庫里面取得),代碼如下:
2.第二步編寫admin.py對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臺管理User中擴(kuò)展一個下拉框和一個多選框,效果如下圖:
以上這篇Django Xadmin多對多字段過濾實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. python中PyQuery庫用法分享3. python操作數(shù)據(jù)庫獲取結(jié)果之fetchone和fetchall的區(qū)別說明4. PHP獲取時間戳等相關(guān)函數(shù)匯總5. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能6. Ajax實(shí)現(xiàn)頁面無刷新留言效果7. php5.6不能擴(kuò)展redis.so的解決方法8. python 爬取嗶哩嗶哩up主信息和投稿視頻9. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效10. AJAX實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺】
