Django formset-如何为modelformset中的每个表单提供不同的查询集

时间:2018-09-11 12:13:17

标签: python django forms django-forms

我有一个模型表格-ClinicallyReportedSample,该表格链接到Sample模型。

我正在尝试为ClinicallyReportedSample创建一个表单集,其中基于Sample的查询集显示特定数量的表单,以便用户可以添加数据。

当前,“样本”模型具有条目,但是ClinicallyReportedSample模型完全为空:

型号:

class Sample(models.Model):

    request_number = models.PositiveIntegerField()
    year = models.PositiveIntegerField()


    class Meta:
        db_table = "sample"
        unique_together = (('request_number', 'year'),)

    def __str__(self):
        return("%s/%s" %(self.request_number, self.year))



class ClinicallyReportedSample(models.Model):

    sample_id = models.ForeignKey(Sample,
                                on_delete=models.CASCADE,
                                db_column='sample_id')


    reported = models.BooleanField(default=False)
    evidence = models.TextField(null=True, blank=True)

    ... other fields ...

    class Meta:
        db_table = "clinically_reported_sample"
        unique_together = (('sample_id'),)

    def __str__(self):
        clinically_reported_sample = str(self.sample_id)
        return(clinically_reported_sample)

我想要一个与样本模型查询集相关的表单集中的ClinicallyReportedSample模型表单。

例如,对具有pk 1、2和3的Sample对象进行采样

forms.py:

class BaseCRSFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # self.queryset = ClinicallyReportedVariant.objects.none()


class CRSForm(forms.ModelForm):

    class Meta:
        model = ClinicallyReportedSample
        fields = ('sample_id', 'evidence',)

    def __init__(self, *args, **kwargs):
        super(CRSForm, self).__init__(*args, **kwargs)

所以我尝试在表单集中使用queryset来做到这一点

views.py:

def get(self, request, *args, **kwargs):


    sample_obj = Sample.objects.filter(id__in=[1, 2, 3])

    formset = modelformset_factory(
                ClinicallyReportedSample,
                form=self.crsform,
                formset=BaseCRSFormSet,
                extra=3,
            )


    formset = formset(queryset=sample_obj)

但是这显示为三种形式,带有所有Sample对象,queryset不起作用。这是解决这个问题的正确方法吗?

1 个答案:

答案 0 :(得分:0)

您需要将默认的Sample查询集设置为none:

class CRSForm(forms.ModelForm):

    class Meta:
        model = ClinicallyReportedSample
        fields = ('sample_id', 'evidence',)

    sample_id = forms.ModelChoiceField(queryset=Sample.objects.none())

    def __init__(self, *args, **kwargs):
        super(CRSForm, self).__init__(*args, **kwargs)

然后在创建表单集实例时手动分配查询集,如下所示:

def get(self, request, *args, **kwargs):

    sample_obj = Sample.objects.filter(id__in=[1, 2, 3])

    formset = modelformset_factory(
                ClinicallyReportedSample,
                form=self.crsform,
                formset=BaseCRSFormSet,
                extra=3,
            )


    formset = formset(queryset=sample_obj)

    for form in formset:
        form.fields['sample_id'].queryset = sample_obj

请注意,您还必须在POST函数中也手动设置查询集,否则它将无法验证。