0

I have a checkbox choice field like this:

models.py
TYPE_CHOICES = (
       ('s', 'small'),
       ('m', 'medium'),
       ('b', 'big'),
     )
class Post(models.Model) :
    size = models.CharField(blank=True,max_length="3", choices=TYPE_CHOICES)

So in the admin form:

class MainContent(forms.ModelForm):
    size = forms.MultipleChoiceField(choices=TYPE_CHOICES, widget=forms.CheckboxSelectMultiple())
    class Meta:
        model = Post
        fields = '__all__'

But when I saving the form it give me this error:

Select a valid choice. ['s', 'm', 'b'] is not one of the available choices.

So, what is the problem?

UPDATE

My apology, this question was not given enough info, let me explain more.

The reason I want a checkbox input is because I want it able to store multiple values in the single field (one column). Perhaps the data can be serialized (or comma separated),

Since the TYPE_CHOICES is static and will not changed in the future, I am not planning to use ManytoMany.

Also I want it able to display in the template easily by the language language.

Hope this is clear enough.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
dev-jim
  • 2,404
  • 6
  • 35
  • 61

2 Answers2

0

Presumably, you want a single column in a table that is storing multiple values. This will also force you to think about how to will serialize - for example, you can't simply do comma separated if you need to store strings that might contain commas.

However, you are best using a solution like one of the following:

https://pypi.python.org/pypi/django-multiselectfield/

https://pypi.python.org/pypi/django-select-multiple-field/

This answer solve your problem, you are try to store a list of strings, but the charfield.

Link to post

or try use ChoiceField

Community
  • 1
  • 1
Paulo Pessoa
  • 2,509
  • 19
  • 30
0

You're using a CharField in your model but in your form you use MultipleChoiceField, for a ModelForm it's an incompatible type because it wouldn't know what to do if the user chooses more than one option, I would recommend using a catalog and a ManyToManyField:

If you want to select more than one size for a Post model:

models.py

class SizesCatalog(models.Model):
    description = models.CharField(max_length=255)

    def __str__(self):
         return self.description

class Post(models.Model) :
    size = models.ManyToMany(SizesCatalog, db_table='post_sizes')

admin form

class MainContent(forms.ModelForm):
    class Meta:
        model = Post
        fields = '__all__'

if you want to select just one size for just one:

models.py

class SizesCatalog(models.Model):
    description = models.CharField(max_length=255)

    def __str__(self):
         return self.description

class Post(models.Model) :
    size = models.ManyToMany(SizesCatalog)

admin form

class MainContent(forms.ModelForm):
    class Meta:
        model = Post
        fields = '__all__'

That way you let Django deal with your problems and in a future you could even update your sizes in a better way

Viktor eXe
  • 620
  • 6
  • 8