如何解决django:处理表单中额外的多对多关系字段
我与自定义数据透视表(通过参数)具有多对多关系以具有其他字段。
在我的表单中,我想使用复选框显示多对多关系。对于每个 ckechbox,我希望能够设置 quantity
参数。
模型:
class Match(models.Model):
# ...Some additional fields...
bonus = models.ManyToManyField(Bonus_type,blank=True,through='Match_bonus')
class Match_bonus(models.Model):
match = models.ForeignKey(Match,on_delete=models.CASCADE)
bonus = models.ForeignKey(Bonus_type,on_delete=models.CASCADE)
quantity = models.IntegerField(blank=True,default=1,validators=[MinValueValidator(0)])
形式:
class MatchForm(forms.ModelForm):
bonus_quantity = forms.IntegerField(validators=[MinValueValidator(0)]) # this is actually wrong because I need one of this field for each option of the ModelMultipleChoiceField
bonus = forms.ModelMultipleChoiceField(queryset=None,widget=forms.CheckboxSelectMultiple)
def __init__(self,*args,tournament_type,**kwargs):
super(MatchForm,self).__init__(*args,**kwargs)
self.fields["bonus"].queryset = Bonus_type.objects.filter(tournament_type=tournament_type)
class Meta:
model = Match
exclude = ()
在我看来,我使用 inlineformset_factory
在表单集中显示表单:
matchFormset = inlineformset_factory(Menu,Match,MatchForm,exclude=['weight_player_1,weight_player_2'],extra=extra_form)
formset = matchFormset(instance=menu,form_kwargs={'tournament_type': tournament.tournament_type})
有什么关于如何执行的提示吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。