在Django中,如何从单个表单字段填充泛型关系?

时间:2016-02-28 19:29:01

标签: python django django-models django-forms django-generic-relations

在我们的应用程序中,我们的模型A包含泛型关系的字段,因为它可能与两个模型之一(BC)的实例相关

填充非泛型ForeignKey字段时,我们可以使用ModelChoiceField。 我们的问题是我们找不到MultipleModelsChoiceField,其选择将从BC的查询集中填充。

如何通过重用尽可能多的现有Django代码来解决这个问题?

1 个答案:

答案 0 :(得分:2)

您可以这样做:

class YourForm(forms.ModelForm):
    your_field = forms.ChoiceField()

    def __init__(self, *args, **kwargs):
        super(YourForm, self).__init__(*args, **kwargs)
        your_generic_relations_objects = list(FirtsModel.object.all()) + list(SecondModel.objects.all())  # and etc
        object_choices = []
        for obj in your_generic_relations_objects:
            type_id = ContentType.objects.get_for_model(obj.__class__).id
            obj_id = obj.id
            form_value = 'type:%s-id:%s' % (type_id, obj_id)
            display_text = str(obj)
            object_choices.append([form_value, display_text])
        self.fields['your_field'] = forms.ChoiceField(choices=object_choices)

    class Meta:
        model = YourModel
        fields = [
            'your_field'  # and others
        ]

    def save(self, *args, **kwargs):
        object_string = self.cleaned_data['your_field']
        matches = re.match("type:(\d+)-id:(\d+)", object_string).groups()
        content_type_id = matches[0]
        object_id = matches[1]
        content_type = ContentType.objects.get(id=content_type_id)
        self.instance.object_id = object_id
        self.instance.content_type = content_type
        return super(YourForm, self).save()