Django Forms, Display Error on ModelMultipleChoiceField

I'm having an issue getting validation error messages to display for a particular field in a Django form, where the field in question is a ModelMultipleChoiceField.

In the clean(self) method for the Form, I try to add the error message to the field like so:

msg = 'error'
self._errors['field_name'] = ErrorList([msg])
raise forms.ValidationError(msg)

This works okay where 'field_name' points to other field types, but for ModelMultipleChoiceField it just won't display. Should this be handled differently?


Asked by: Emma954 | Posted: 05-10-2021






Answer 1

Yeah, it sounds like you're doing it wrong.

You should be using the clean_ method instead. Read through that whole document, in fact - it's very informative.

Answered by: Emily578 | Posted: 06-11-2021



Answer 2

Why are you instantiating an ErrorList and writing to self._errors directly? Calling "raise forms.ValidationError(msg)" takes care of all that already.

And what does your template look like?

Answered by: Maria897 | Posted: 06-11-2021



Similar questions

python - Display labels of ModelMultipleChoiceField

How to display the labels of the choices in a ModelMultipleChoiceField ? This is the form : class MakeAnAppointmentForm(forms.Form): time_slot = forms.ModelMultipleChoiceField(queryset = None, widget=forms.CheckboxSelectMultiple()) def __init__(self,*args,**kwargs): date = kwargs.pop('date') super().__init__(*args,**kwargs) self.fields['time_slot'].queryset = Appointme...


python - Display labels of ModelMultipleChoiceField

How to display the labels of the choices in a ModelMultipleChoiceField ? This is the form : class MakeAnAppointmentForm(forms.Form): time_slot = forms.ModelMultipleChoiceField(queryset = None, widget=forms.CheckboxSelectMultiple()) def __init__(self,*args,**kwargs): date = kwargs.pop('date') super().__init__(*args,**kwargs) self.fields['time_slot'].queryset = Appointme...


python - Django: ModelMultipleChoiceField doesn't select initial choices

ModelMultipleChoiceField doesn't select initial choices and I can't make the following fix (link below) work in my example: http://code.djangoproject.com/ticket/5247#comment:6 My models and form: class Company(models.Model): company_name = models.CharField(max_length=200) class Contact(models.Model): ...


python - django ModelMultipleChoiceField (default values)

I've been searching and trying to get this to work for the past few hours and I can't I have a list of skills that I take from the database: forms.ModelMultipleChoiceField( skills.objects.all(), required=True, widget=forms.CheckboxSelectMultiple(), label='Check your skills.', ...


python - Django ModelMultipleChoiceField : Iterating through the data

I am trying to display the data from a ModelMultipleChoiceField, and have tried to iterate for the objects, but I am getting funky results. Thanks for the help! {{ chef.meal }} Renders [<Meal: Breakfast>, <Meal: Brunch>] -- {% for i in chef.meal%} {{i}} {% endfor %} Renders [ < M e a l : B ...


python - How to display the options' label in ModelMultipleChoiceField?

I have this: class HouseForm(forms.ModelForm): amenities = ModelMultipleChoiceField( queryset=Amenity.objects.all(), widget=forms.CheckboxSelectMultiple(), required=False ) Is there a way I can construct my own list of checkboxes? Instead of the default in unordered list? Here's what I hope to achieve: <select> {% for a in hous...


python - Question about ModelMultipleChoiceField

It so happened that I had to use arrays of PostgreSQL. In Django models do not have native support for arrays, so I used django_arrayfields. But for display in the admin should I use for the field hoprizontal_filter IntegerArrayField. models.py class Group(models.Model): name = models.TextField() class User(models.Model): ...


python - Saving to ManyToManyFields using ModelForm and ModelMultipleChoiceField

I've created a basic Django app that contains books/authors/publishers as per the Django Book - trying to use a ModelForm to create a means to modify existing books - the problem is that the 'authors' field is a ManyToManyField and when I choose a choice on the ModelForm it simply wipes the existing selection and doesn't save the new one? models.py class Book(models.Model): title = models.Ch...


python - django form add css to ModelMultipleChoiceFIeld

How exactly do I add css to a forms.ModelMultipleChoiceField I tried to pass in attrs={'class' : 'foo'} But it's failing saying "__init__() got an unexpected keyword argument 'attrs'" my_list = forms.ModelMultipleChoiceField(label='List', required=False, queryset=[])


python - django 'too many SQL variables' error with ModelMultipleChoiceField

I have a form with ModelMultipleChoiceField on it, and I am getting 'DatabaseError: too many SQL variables' (using SQLite) when the user picks more than 1000 entries in the selection widget and posts the form. The problem seems to be the method clean of ModelMultipleChoiceField, which tries to select objects from the database simply by the IN SQL clau...


python - DJANGO: Add "select all" choice option to ModelMultipleChoiceField?

Is there a way to add additional choices to a ModelMultipleChoiceField? Specifically, I want to add a "Select All" option instead of an End-User having to select all of the choices individually. Current Model Form: class QueryForm(forms.Form): book = forms.ModelMultipleChoiceField(queryset=Book.objects.all()) I want something like this: class QueryForm(forms.For...


python - django modelmultiplechoicefield and widget checkboxselectmultiple

I would like to have something like the admin interface. here is the code for the form: class NewRoleFrom(forms.Form): role = forms.ModelMultipleChoiceField( queryset=Role.objects.all(), widget=forms.CheckboxSelectMultiple ) So, it's simple, I have the Role label(Role:) then each role in the database is rendered with a checkbox. Like that I can get back al...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top