1

I have the following models:

POSSIBLE_POSITIONS = (
    (1, 'Brazo'),
    (2, 'Muñeca'),
    (3, 'Pierna'),
    (4, 'Pie'),
)

class Device(models.Model):
    id = models.CharField(max_length=10, primary_key=True)

class User(models.Model):
    id = models.CharField(max_length=10, primary_key=True)
    device = models.ForeignKey(Device, blank=True, null=True, on_delete=models.SET_NULL)
    position = models.CharField(max_length=1, choices=POSSIBLE_POSITIONS, default=1)

I would like to know how to work with that dictionary. If I go to Django shell and I do the following it works:

user = User(pk=1, position=12312)

Why this work? 12312 is not a possible choice from POSSIBLE_POSITIONS.

How can I exchange the number for the String?

Also, how can I show all this options on a selector when creating a new user via html?

Lechucico
  • 1,914
  • 7
  • 27
  • 60

1 Answers1

0
user = User(pk=1, position=12312)

This is not showing any error because when you are initializing the object the value is not validated, but when you will try to save the object it will shows error. You need to user

forms.ChoiceField(choices=POSSIBLE_POSITIONS, widget=forms.Select()) 

options on a selector.