Django 2.0.9 - Multiselectformfield - Parameter to make not mandatory

Asked

Viewed 314 times

0

I want to take the field Multiselectfield not mandatory. When I do not choose any of the options the validation is charging mandatory field. I tried to use the parameters Blank=True and null=True, but still accuse that the field is required.

Does anyone know which parameter I should inform for the field Multiselectfield be optional and not mandatory?

Forms.py

    class MyForm(forms.ModelForm):

        MY_CHOICES = (
            ('1', "A good choice"),
            ('2', "A bad choice"),
        )

        my_field = forms.MultipleChoiceField(
               label='Escolhas', widget=forms.CheckboxSelectMultiple, choices=MY_CHOICE
               )

py.models

from multiselectfield import MultiSelectField

class MyModel(models.Model):
    MY_CHOICES = (
        ('1', "A good choice"),
        ('2', "A bad choice"),
    )
    my_field = MultiSelectField('Escolhas', choices=MY_CHOICES, default=0)
  • Gabriel, the title of your question specifies that it is [tag:Django-2.0], but you have put the tag [tag:Django-1.8]. Could you clarify which of the two is what you’re using??

  • I changed kkkk here. I am using the version of Django 2.0.9

1 answer

0

Guy, in the documentation there’s no Multiselectfield, so I guess you’re using Django-multiselectfield (it would be nice to specify in the question the lib and the version).

Seeing the code, you can see that the class extends from models.CharField and uses the comma-separated fields underneath the cloths. You can also see that it implements the properties min_choices and max_choices.

I never used this library, but I would try to use blank=True and min_choices=0.

Ex.:

my_field = MultiSelectField('Escolhas', choices=MY_CHOICES, blank=True, min_choices=0)
  • I’m using the lib Django-multiselectfield, sorry I don’t specify the lib version, I don’t even know how I see it. It makes sense what I said, I’ll try to do it and I’ll get back to you. Thank you!

  • It’s just a recommendation for your question to have more quality. Questions with more quality tend to have better answers. Mine, for example, is liable to take a few downvotes for being vague. But since I didn’t work with the library is my limit on what I can help. D

  • Blank=True, min_choices=0 did not work, but I put required=False and the field is no longer required. Value!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.