Choices values are being stored like this ['Dog', 'Cat', 'Birds']

Asked

Viewed 145 times

0

I have a system that will have the option of choosing 1 or multiple options in the same field. Bad problem is that I need my field inside the model to be charfield and in Forms to be Multiplechoicefield to be able to validate multiple choices. I know I could use the filedmultiple in the model and it would work, but that means Dari error in Django filter. I need that as the model is now, both Forms and model, but that it goes into the clean values and not as it is passing now, now passes like this:['Dog', 'Cat', 'Birds']

py.models

PETN_CHOICES = (
    ('Cachorro','Cachorro'), ('Gato','Gato'), ('Pássaros', 'Pássaros'), ('Peixes','Peixes'), ('Reptéis','Reptéis'), ('Roedores','Roedores')
)
class Negocio(models.Model):
   pet_aceitos = models.CharField(max_length=255, choices=PETN_CHOICES)

Forms.py

PETN_CHOICES = (
    ('Cachorro','Cachorro'), ('Gato','Gato'), ('Pássaros', 'Pássaros'), ('Peixes','Peixes'), ('Reptéis','Reptéis'), ('Roedores','Roedores')
)

class NegocioForm(UserCreationForm):
    pet_aceitos = forms.MultipleChoiceField(
          widget=forms.CheckboxSelectMultiple, choices=PETN_CHOICES, )

1 answer

0

I understand you have a form receiving FIXED CHOICES. Inside your MODEL Business the field accepted You don’t have to have CHOICES. You can make this user "lock" only enter the data you want in the form as you already did, and convert it to SRT in the view that is the form. See if it helps :)

Browser other questions tagged

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