I want that one of the fields of the model can take only some specific values say a and b. This can be achieved normally in sql by adding the 'IN' constraint. How can I achieve the same using django model class fields.
eg.
Some field say fruits can have values 'banana' and 'orange' only
Please help. I am new to django.
You're referring to choices.
Example from the docs:
from django.db import models
class Student(models.Model):
FRESHMAN = 'FR'
SOPHOMORE = 'SO'
JUNIOR = 'JR'
SENIOR = 'SR'
YEAR_IN_SCHOOL_CHOICES = (
(FRESHMAN, 'Freshman'),
(SOPHOMORE, 'Sophomore'),
(JUNIOR, 'Junior'),
(SENIOR, 'Senior'),
)
year_in_school = models.CharField(max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES,
default=FRESHMAN)