Python中文
首页
教程
问答
标签
搜索
登录
注册
Django MultiChoiceField to_field_name和selected
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我目前正在使用一个多选择域来表示我的领域中相应的ManyToManyField。它工作得很好。在</p> <p>我更新了我的表单,在表单域中使用属性to_field_name来更改用于输入值的字段,它也可以正常工作。在</p> <p>我的问题是Django选择表单字段的所选数据时,使用的是pk,而不是tou field_name中给定的字段。有什么线索吗?在</p> <pre><code>class Model1(models.Model): name = models.Charfield(...) muscles = models.ManyToManyField(Model2, blank=True, null=True,) class Model2(models.Model): name = models.CharField(...) field1 = models.CharField(...) class MyForm(ModelForm): muscles = forms.ModelMultipleChoiceField( queryset=None, to_field_name="field1", required=False,) def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['muscles'].queryset = Model2.objects.all() </code></pre> <p>模型的字段是一个标准的多个字段。在</p> <p>例如:</p> <p>在model2中,我有以下元素:{pk=1,name=name1,field1=3},{pk=2,name=name2,field1=1},{pk=3,name=name3,field1=2}。在</p> <p>生成的select是:</p> ^{pr2}$ <p>如果我选择name2,正确的pk(即2)将保存在数据库中。 显示表单时,所选选项为name3(即值2),而不是name1。在</p> <p>我希望我已经足够清楚了。在</p> <p>谢谢你的帮助!在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>看起来您可能已经点击了<a href="https://code.djangoproject.com/ticket/17657#comment:13" rel="nofollow">this bug</a>,这在django1.10中已修复。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在乒乓球比赛中预测球的轨迹,对于AI球拍预测?
8 回答
如何在乒乓球游戏中阻止球
2 回答
如何在乘法和模中不乘空间?
6 回答
如何在乘法和除以2个不同的数字之间进行交换?
8 回答
如何在也是数据一部分的单个字符上拆分大字符串
6 回答
如何在乾草堆中找到針,有更好的解決方案嗎?
6 回答
如何在事件wxWidgets中传递自定义数据
3 回答
如何在事件中使用lambda i=i?
4 回答
如何在事件中心只接收最近的数据
4 回答
如何在事件发生之前保持云函数运行?
3 回答
如何在事件发生后使页面重定向到同一页面
7 回答
如何在事件回调之间保持python生成器的状态
6 回答
如何在事件处理程序(pythonsocket、sphinx)中保留docstring
9 回答
如何在事件处理程序中更改wxRichTextCtrl的光标位置?
1 回答
如何在事件处理程序中访问外部对象?
8 回答
如何在事件循环中将协程打包为正常函数?
2 回答
如何在事件循环之外运行协同程序?
10 回答
如何在事件循环结束时为并发未来的所有线程调用类方法?
9 回答
如何在事件文件中只保留一份摘要?
10 回答
如何在事件模板中添加事件
4 回答