Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want that Django model field can have only two possible values

Tags:

django

class Proposta(models.Model):
     descrizione = models.TextField()
     titolo = models.CharField(max_length=200)
     richiedibile = models.BooleanField(default=False)
     inRichiesta = models.BooleanField(default=False)
     archiviata = models.BooleanField(default=False)

     # tesi or AP
     tipologia = models.CharField(max_length=50)

I want that the 'tipologia' field can have only two possible value: 'tesi' or 'AP'. In other words I want the field looks like a list in which user can choose the value he want.

like image 613
Tajinder Singh Avatar asked Sep 20 '17 10:09

Tajinder Singh


1 Answers

Use the choices argument in your Charfield:

TIPOLOGIA_CHOICES = [
    ("tesi", "tesi"),
    ("AP", "AP"),
]

class Proposta(models.Model):
    ...
    tipologia = models.CharField(max_length=50, choices=TIPOLOGIA_CHOICES)
like image 143
Alasdair Avatar answered Sep 22 '22 13:09

Alasdair