gender_choices = models.CharField[('M', 'Male'), ('F','Female')]
gender = models.CharField(choices=gender_choices, max_length=1, default=None, null=True)
CodePudding user response:
You can use as
gender_choices = (('M', 'Male'), ('F','Female'))
gender = models.CharField(choices=gender_choices, max_length=1, default=None, null=True)
Note : The first element in each tuple is the actual value to be set and second element is name.
CodePudding user response:
For the choices you should construct a simple list, not use CharField
, so:
class SomeModel(models.Model):
GENDER_CHOICES = [('M', 'Male'), ('F','Female')]
gender = models.CharField(
choices=GENDER_CHOICES,
max_length=1,
default=None,
null=True
)