django widget radio button error save is not touch

14 views
Skip to first unread message

carlos

unread,
Sep 6, 2020, 8:51:02 PM9/6/20
to django...@googlegroups.com
Hi, i have a problem when save a field in radio button
in my models have this
CHOICES_OPTIONS = (
 (1, 'Yes'),
(2, 'No'),
)

class MyModels(...):
   myfield = models.IntegerField(choices=CHOICES_OPTIONS, null=True, blank=True)
   ....

class MyModelsForm()
   myfield = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES_OPTIONS,
   required=False)

but when save the forms in my views this sends this error:
Field 'myfield' expected a number but got ''.
sometimes the user does not select anything neither yes nor no, the radio button they won't touch it

How can I make that null field be saved without any value and it does not throw me that error

José Luis Ruiz

unread,
Sep 7, 2020, 9:38:24 AM9/7/20
to Django users
Build your class like this one:

class Card(models.Model): 
  class Suit(models.IntegerChoices): 
  DIAMOND = 1 
  SPADE = 2 
  HEART = 3 
  CLUB = 4 
  suit = models.IntegerField(choices=Suit.choices)

https://docs.djangoproject.com/en/3.1/ref/models/fields/#django.db.models.Field.choices

Look at the middle of the page

Reply all
Reply to author
Forward
0 new messages