如何将TypedChoiceField作为表单类分配给带有选择的模型字段?

时间:2021-07-12 19:36:00

I had CharField with choices in the model, but I need for this field render as CheckboxSelectMultiple, which returns list to a form class. With TypedChoiceField form class, which automatic assigned to Field with choices, it's not validate. I decided to change form_class in the field and written a new field with TypedMultipleChoiceField form_class, to validate list.

我在模型中有带有选项的CharField,但是我需要将这个字段呈现为CheckboxSelectMultiple,它将列表返回给一个表单类。对于TypedChoiceField表单类,它自动分配给带有选项的字段,它不进行验证。我决定在字段中更改form_class,并使用TypedMultipleChoiceField form_class编写一个新的字段,以验证列表。

class MultipleTypedChoiceModelField(models.Field):
    def get_internal_type(self):
        return 'MultipleTypedChoiceModelField'

    def formfield(self, **kwargs):
        defaults =  {'form_class': TypedMultipleChoiceField, }
        defaults.update(kwargs)
        return super(MultipleTypedChoiceModelField, self).formfield(**defaults)

But it is have no effect. If I commented out the choices in the model field, the type is MultipleTypedChoiceModelField. So I believe that the form_class is appointed in appliance with a list of choices definition.

但它没有效果。如果我在模型字段中注释掉了选项,类型是MultipleTypedChoiceModelField。因此,我认为form_class是在具有选择定义列表的设备中指定的。

def formfield(self, **kwargs):
    if self._choices: # or self.choices:
        defaults =  {'form_class': TypedMultipleChoiceField, }
        defaults.update(kwargs)
    return super(MultipleTypedChoiceModelField, self).formfield(**defaults)

But it is have no effect too. I have not found where is the assignment of form_class. Maybe I have better way to change this behavior? Adding an extra model with many to many relation I do not plan, because it is obviously unnecessary.

但它也没有效果。我还没找到form_class的赋值在哪里。也许我有更好的方法来改变这种行为?我不打算在许多关系中添加一个具有多个关系的额外模型,因为它显然是不必要的。

1 个解决方案

#1


1  

Part of the problem is that if you have something like this in your model:

部分问题是如果你的模型中有这样的东西

myfield = models.CharField(max_length=100, choices=MY_FIELD_CHOICES)

Django is going to assume that myfield holds just one choice, nut multiple choices.

Django假设myfield只包含一个选项,但有多个选项。

Easiest way to implement this is to just have something like this in your model:

最简单的实现方法是在模型中有这样的东西:

myfield = models.CharField(max_length=100)

and then in your custom form:

然后在你的定制表格中:

myfield = forms.MultipleChoiceField(choices=MY_FIELD_CHOICES, widget=forms.CheckboxSelectMultiple())

#1


1  

Part of the problem is that if you have something like this in your model:

部分问题是如果你的模型中有这样的东西

myfield = models.CharField(max_length=100, choices=MY_FIELD_CHOICES)

Django is going to assume that myfield holds just one choice, nut multiple choices.

Django假设myfield只包含一个选项,但有多个选项。

Easiest way to implement this is to just have something like this in your model:

最简单的实现方法是在模型中有这样的东西:

myfield = models.CharField(max_length=100)

and then in your custom form:

然后在你的定制表格中:

myfield = forms.MultipleChoiceField(choices=MY_FIELD_CHOICES, widget=forms.CheckboxSelectMultiple())