在Django inlineformset_factory中过滤Queryset

时间:2010-04-05 20:42:24

标签: django django-forms

我正在尝试使用inlineformset_factory来生成formset。我的模型定义为:

class Measurement(models.Model):
    subject = models.ForeignKey(Subject)
    experiment = models.ForeignKey(Experiment)
    assay = models.ForeignKey(Assay)
    values = models.CommaSeparatedIntegerField(blank=True, null=True)

class Experiment(models.Model):
    date = models.DateField()
    notes = models.TextField(max_length = 500, blank=True)
    subjects= models.ManyToManyField(Subject)

在我看来,我有:

def add_measurement(request, experiment_id):
    experiment = get_object_or_404(Experiment, pk=experiment_id)
    MeasurementFormSet = inlineformset_factory(Experiment, Measurement, extra=10, exclude=('experiment'))
    if request.method == 'POST':
        formset = MeasurementFormSet(request.POST,instance=experiment)
        if formset.is_valid():
            formset.save()
            return HttpResponseRedirect( experiment.get_absolute_url() ) 
    else:
        formset = MeasurementFormSet(instance=experiment)
    return render_to_response("data_entry_form.html", {"formset": formset, "experiment": experiment }, context_instance=RequestContext(request))

但我想将Measurement.subject字段限制为只在Experiment.subjects查询集中定义的主题。我尝试了几种不同的方法,但我不确定实现这一目标的最佳方法是什么。我试图用新的查询集覆盖BaseInlineFormset类,但无法弄清楚如何正确传递实验参数。

更新回答(我还在此处提供了将参数传递给formset link的方法):

views.py

def add_measurement(request, experiment_id):    
    experiment = get_object_or_404(Experiment, pk=experiment_id)    
    MeasurementFormSet = inlineformset_factory(Experiment, Measurement, extra=10, can_delete=True, form=MeasurementForm)    
    MeasurementFormSet.form = staticmethod(curry(MeasurementForm, experiment=experiment))
    if request.method == 'POST':
        formset = MeasurementFormSet(request.POST)      
        if formset.is_valid():
        formset.save()
        return HttpResponseRedirect( experiment.get_absolute_url() )    
    else:
        formset = MeasurementFormSet()
        return render_to_response("data_entry_form.html", {"formset": formset, "experiment": experiment }, context_instance=RequestContext(request))

forms.py

class MeasurementForm(ModelForm):
    class Meta:
        model = Measurement
    def __init__(self, *args, **kwargs):
        experiment = kwargs.pop('experiment')
        super(MeasurementForm, self).__init__(*args, **kwargs)
        self.fields["subject"].queryset = Subject.objects.filter(experiment=experiment)

2 个答案:

答案 0 :(得分:3)

(编辑:没有正确读取代码块,这里应该是您问题的解决方案):

我相信你需要:http://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield

<强> Forms.py:

class MeasurementForm(ModelForm):
 subject = forms.ModelChoiceField(queryset = Expirement.objects.all())
 class Meta:
   model = Measurement

<强> Views.py:

inlineformset_factory(
  Experiment, Measurement, extra=10, 
  exclude=('experiment'), form=MeasurementForm
  )

使用表单参数绑定到formset。

答案 1 :(得分:1)

我遇到了同样的问题(使用有限的可能值初始化inlineforms),并且更新后的答案效果很好。感谢那。 无论如何,我认为有些事情可以做得更好,但我不知道如何完成它。该解决方案中的新问题是您在每个内联形式中访问数据库:而不是在所有相同的字段中使用相同的查询集,每次在此行中重新计算它:

 self.fields["subject"].queryset = Subject.objects.filter(experiment=experiment)

我在这个问题上是对的还是在引擎盖后面有一些懒惰的django-magic?如果我是对的,我怎么能避免DB的可能百分之一? 问候, 佩德罗