2016-11-05 21:12:12 +08:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
class Product(models.Model):
|
2018-08-06 10:30:44 +08:00
|
|
|
price = models.IntegerField(null=True)
|
|
|
|
discounted_price = models.IntegerField(null=True)
|
2016-11-05 21:12:12 +08:00
|
|
|
|
|
|
|
class Meta:
|
2019-08-10 14:41:18 +08:00
|
|
|
required_db_features = {
|
|
|
|
'supports_table_check_constraints',
|
|
|
|
}
|
2016-11-05 21:12:12 +08:00
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
2018-08-06 10:15:10 +08:00
|
|
|
check=models.Q(price__gt=models.F('discounted_price')),
|
|
|
|
name='price_gt_discounted_price',
|
|
|
|
),
|
2019-07-05 20:15:41 +08:00
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.Q(price__gt=0),
|
|
|
|
name='%(app_label)s_%(class)s_price_gt_0',
|
|
|
|
),
|
2019-08-10 14:41:18 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class UniqueConstraintProduct(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
color = models.CharField(max_length=32, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
2018-12-28 03:21:59 +08:00
|
|
|
models.UniqueConstraint(fields=['name', 'color'], name='name_color_uniq'),
|
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=['name'],
|
|
|
|
name='name_without_color_uniq',
|
|
|
|
condition=models.Q(color__isnull=True),
|
|
|
|
),
|
2016-11-05 21:12:12 +08:00
|
|
|
]
|
2019-07-05 20:15:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AbstractModel(models.Model):
|
|
|
|
age = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2019-08-10 14:41:18 +08:00
|
|
|
required_db_features = {
|
|
|
|
'supports_table_check_constraints',
|
|
|
|
}
|
2019-07-05 20:15:41 +08:00
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.Q(age__gte=18),
|
|
|
|
name='%(app_label)s_%(class)s_adult',
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ChildModel(AbstractModel):
|
|
|
|
pass
|