djangodjango-modelscheck-constraints

How to add a check constraint in django model that a field value startwith letter 'c' or 'e' or 'a'


How to add a check constraint in django model that a field value startwith letter 'c' or 'e' or 'a' like the bellow SQL check constraint

CREATE TABLE Account (
    account_no varchar(12),
    FirstName varchar(255),
    Age int,
    City varchar(255),
    CONSTRAINT CHK_Person CHECK (SUBSTR(account_no,1,1) = 'c' OR SUBSTR(account_no,1,1) = 'e' OR SUBSTR(account_no,1,1) = 'a' )
); 

i try with meta class of model. but i don't know how to specify the or case

  class Meta:
        constraints = [
            CheckConstraint(
                check = Q(account_no___startswith=F('')), 
                name = 'check_start_wtih',
            ),
        ]

Solution

  •   class Meta:
            constraints = [
                CheckConstraint(
                    check = Q(account_no___startswith='c') | Q(account_no___startswith='e') | Q(account_no___startswith='a'), 
                    name = 'check_start_wtih_cea',
                ),
            ]