更改非父模型的inlineformset模型字段的查询 [英] Change queryset of model field in inlineformset of non parent model

查看:87
本文介绍了更改非父模型的inlineformset模型字段的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Foo(Model):
name = models.TextField()

class Bar(Model):
foo = models.ForiegnKey(Foo)
other_model = models.ForeignKey(OtherModel)

class BarForm(ModelForm):
class Meta:
model = Bar


foo = Foo.object.get(id = 1)
FormSet = inlineformset_factory(Foo,Bar,form = BarForm)
formset = FormSet(instance = foo)

根据foo的值,直到我输入视图代码为止,我需要更改other_model字段的查询表单中的所有表单的BarForm。有没有办法做到这一点?

解决方案

如果我正确理解你,这是你可以做的...你可以覆盖 BaseInlineFormSet ,然后在表单中的每个表单上手动设置该字段的查询集。



所以在你的forms.py中,你会这样做:

  class BaseBarFormSet(BaseInlineFormSet):
def __init __(self,other_model_queryset,* args,** kwargs):
super(BaseInlineFormSet,self).__ init __(* args,** kwargs)

for self.forms中的表单:
form.fields ['other_field'] queryset =另请注意,__init__的第一个参数是要设置的查询集。

$ / $ $ $ $ $ $ $ $ $ $ $ $

然后在你看来,你只需要修改你当前的代码。在工厂函数中传入新的BaseBarFormSet:

  FormSet = inlineformset_factory(Foo,Bar,form = BarForm,formset = forms。 BaseBarFormSet)#notice formset = forms.BaseBarFormSet 

然后将您想要的其他字段的查询集传递给实际 FormSet 由工厂函数创建的类:

  formset = FormSet OtherModel.objects.filter(...),instance = foo)#notice第一个参数

复杂的时候,所以希望这是有道理的...让我知道,如果你有问题。


I am using an inline formset and need to change the queryset of one of the non parent model's form fields when the formset is instantiated.

class Foo(Model):
   name = models.TextField()

class Bar(Model):
   foo = models.ForiegnKey(Foo)
   other_model = models.ForeignKey(OtherModel)

class BarForm(ModelForm):
   class Meta:
      model=Bar


foo = Foo.object.get(id=1)
FormSet = inlineformset_factory(Foo, Bar, form=BarForm)
formset = FormSet(instance=foo)

Depending on the value of foo which isn't determined until I enter the view code, I need to alter the queryset of the 'other_model' field in the BarForm for all forms in the formset. Is there a way to do this?

解决方案

If I understand you correctly, this is what you can do... You can override BaseInlineFormSet, then manually set the query set for that field on every form in the formset.

So in your forms.py, you would do this:

class BaseBarFormSet(BaseInlineFormSet):
    def __init__(self, other_model_queryset, *args, **kwargs):
        super(BaseInlineFormSet, self).__init__(*args, **kwargs)

        for form in self.forms:
            form.fields['other_field'].queryset = other_model_queryset

Note how the first argument to __init__ is the queryset you want to set.

Then in you view, you would just modify your current code accordingly. Pass in that new BaseBarFormSet in the factory function:

FormSet = inlineformset_factory(Foo, Bar, form=BarForm, formset=forms.BaseBarFormSet) # notice formset=forms.BaseBarFormSet

Then pass the queryset you want for the other field to the actual FormSet class created by the factory function:

formset = FormSet(OtherModel.objects.filter(…), instance=foo) #notice the first parameter

Formsets are very complicated sometimes, so hopefully that made sense… let me know if you have problems.

这篇关于更改非父模型的inlineformset模型字段的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆