Django Xadmin多对多字段过滤实例
作者:宅神kin 时间:2023-02-25 21:03:59
在xadmin中是不能像原生admin那样使用formfield_for_manytomany方法来过滤多对多字段
进入xadmin源码,找到了formfield_for_dbfield这个方法,测试是有用的,可以过滤第一个选项框的值
补充知识:给django admin后台管理user扩展下拉框及多选框的字段
1.首先在models.py中编写扩展User所用到的userProfile模型及下拉框和多选框选项值所需要的模型(因为我所做的下拉框和多选框的值都是从数据库里面取得),代码如下:
2.第二步编写admin.py对User字段进行扩展,代码如下:
# -*- coding: UTF-8 -*-
from django.contrib import admin
from django import forms
from TESTAPP.models import test,userProfile
from django.contrib.auth.admin import UserAdmin
from 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 = userProfileForm
class testUserAdmin(UserAdmin):
inlines = [profileInline,]
admin.site.unregister(User)
admin.site.register(User, testUserAdmin)
通过这两步就可以试下在django admi台管理User中扩展一个下拉框和一个多选框,效果如下图:
来源:https://blog.csdn.net/weixin_42042680/article/details/85158293
标签:Django,Xadmin,多对多,字段,过滤
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
常用的Python代码调试工具总结
2023-05-17 19:24:29
![](https://img.aspxhome.com/file/2023/2/112612_0s.png)
go语言数据结构之前缀树Trie
2023-08-05 18:15:50
![](https://img.aspxhome.com/file/2023/8/97158_0s.png)
python使用python-pptx删除ppt某页实例
2022-10-23 16:19:59
python用类实现文章敏感词的过滤方法示例
2022-04-10 19:30:25
![](https://img.aspxhome.com/file/2023/9/95309_0s.png)
asp.net微信开发(永久素材管理)
2023-07-21 13:02:45
![](https://img.aspxhome.com/file/2023/9/78999_0s.png)
Python初识逻辑与if语句及用法大全
2023-11-21 16:17:24
![](https://img.aspxhome.com/file/2023/7/117327_0s.jpg)
js实现向右横向滑出的二级菜单效果
2024-04-17 10:34:44
![](https://img.aspxhome.com/file/2023/9/136229_0s.jpg)
python套接字socket通信
2023-01-26 17:51:03
![](https://img.aspxhome.com/file/2023/5/81875_0s.png)
Django分页查询并返回jsons数据(中文乱码解决方法)
2022-12-02 22:44:20
python递归打印某个目录的内容(实例讲解)
2021-02-13 00:54:27
Python装饰器与线程结合提高接口访问效率方法
2021-07-16 11:28:32
Python2.X/Python3.X中urllib库区别讲解
2022-08-03 01:45:39
Perl中的10个操作日期和时间的CPAN模块介绍
2023-07-27 10:04:41
页面中横排布局的思考
2008-01-18 12:56:00
![](https://img.aspxhome.com/file/UploadPic/20081/18/200811813731407s.jpg)
js数组与字符串的相互转换方法
2023-06-24 09:41:48
Python面向对象程序设计之继承、多态原理与用法详解
2023-07-08 18:17:52
![](https://img.aspxhome.com/file/2023/6/82716_0s.png)
老生常谈CSS网页布局的意义与副作用
2008-09-12 12:31:00
python和pygame实现简单俄罗斯方块游戏
2022-10-04 20:00:49
![](https://img.aspxhome.com/file/2023/2/69532_0s.gif)
Python中logging日志模块代码调试过程详解
2021-03-13 05:40:32
浅谈GO中的Channel以及死锁的造成
2024-04-23 09:41:29
![](https://img.aspxhome.com/file/2023/4/134364_0s.jpg)