How to set choice limit of given number on ModelMultipleChoiceField in Django?

778 views
Skip to first unread message

inoyon artlover KLANGRAUSCH

unread,
Dec 4, 2014, 9:54:09 PM12/4/14
to django...@googlegroups.com
Hi there, I got a following Form-Class:


class CustomUserprofileInterestsForm(forms.ModelForm):

    interests = forms.ModelMultipleChoiceField(
        queryset=Interests.objects.all(),
        widget=forms.CheckboxSelectMultiple)


I want to limit the choices for example to 6 of all displayed.
Is it possible with some optional arguments I don't know somehow?

Best regards.. cheers!

Larry Martell

unread,
Dec 4, 2014, 10:01:15 PM12/4/14
to django...@googlegroups.com
You can write your own clean method on the form, e.g.:

def clean_interests(self):
value = self.cleaned_data['interests']
if len(value) > 6:
raise forms.ValidationError("You can't select more than 6 items.")
return value

See: https://docs.djangoproject.com/en/1.7/ref/forms/validation/#form-and-field-validation

inoyon artlover KLANGRAUSCH

unread,
Dec 4, 2014, 11:46:44 PM12/4/14
to django...@googlegroups.com
Great, it works with one form but not with an another...
Btw. how is it possible to overwrite the 'this field is required' error message?

Many thanks and best regards! :)

Larry Martell

unread,
Dec 4, 2014, 11:52:18 PM12/4/14
to django...@googlegroups.com
On Thu, Dec 4, 2014 at 6:46 PM, inoyon artlover KLANGRAUSCH
<inoyona...@googlemail.com> wrote:
> Great, it works with one form but not with an another...
> Btw. how is it possible to overwrite the 'this field is required' error
> message?

You can provide your custom set of default errors to the form field
definition, e.g.:

my_default_errors = {
'required': 'You better enter this field!',
'invalid': 'You can do better than that!'
}

class MyForm(forms.Form):
some_field = forms.CharField(error_messages=my_default_errors)

inoyon artlover KLANGRAUSCH

unread,
Dec 5, 2014, 12:20:13 AM12/5/14
to django...@googlegroups.com
Very cool! :) Many thanks, once more! :) Could be off-topic, but there is somethin more tricky:
Before a ManyToManyField there is one ForeignKeyField which has few of the same values.
So the ForeignKey-value has to be excluded from the ManyToMany choices. For example
the ForeignKey selection (labeld as: additional values) could trigger on submit a queryset and
a view colud display the filterd ManyToManyField... (btw. I am very new to python/django so..
try'n error is the tedious way to go since two month... )

Collin Anderson

unread,
Dec 6, 2014, 5:25:59 PM12/6/14
to django...@googlegroups.com
Hi,

Are you saying you have a ForeignKey choice field on the same form (above the ManyToMany), and when you change the choice field it should change the available values in the ManyToMany?

Collin

inoyon artlover KLANGRAUSCH

unread,
Dec 7, 2014, 5:37:22 AM12/7/14
to django...@googlegroups.com
Hi Collin, yes exactly. It is as follows:
User can select a main coummunication language which is connected as ForeignKey to the main langauges table.
Withn the field below the user can select further languages but when I select english as main language,
this language shouldn't be available within the ManyToMany..

Collin Anderson

unread,
Dec 9, 2014, 1:34:36 PM12/9/14
to django...@googlegroups.com
Hi,

On the html form, you could probably use javascript to filter out the value in the many to many when you select the primary language.

In the django form, you could add extra validation by defining form.clean_further_languages(self)

Collin
Reply all
Reply to author
Forward
0 new messages