Home > Back-end >  i was trying to create django model and i got this error TypeError: 'type' object is not s
i was trying to create django model and i got this error TypeError: 'type' object is not s

Time:03-02

    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
    )
  • Related