user10338966
user10338966

Reputation:

Django: multiple different fields -> one model

I want to show different fields (a html-option-field who gets Mymodel.object.all and a textfield) and save it to one model field.

How can I build this?

MultiValueField (https://docs.djangoproject.com/en/2.1/ref/forms/fields/) doesn't help with different fields? Has someone an example? How can I define which kind of field it is?

EDIT: How can I determine which field I want to save in the model-field? I use a ModelForm.

Upvotes: 0

Views: 63

Answers (1)

Kryštof Řeháček
Kryštof Řeháček

Reputation: 2483

You should use forms.ModelChoiceField(choices=ModelClass.objects.all()) for the choicefield, you can also set the widget to be widget=forms.CheckboxSelectMultiple.

your form can be like

class SuperForm(forms.Form):
    cool_field = forms.ModelChoiceField(
        choices=ModelClass.objects.all(),
        widget=forms.CheckboxSelectMultiple,
    )

    text_area = forms.CharField(widget=forms.Textarea)

Upvotes: 1

Related Questions