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-11-15 13:08:36 +08:00
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.expressions.RawSQL(
|
|
|
|
'price < %s', (1000,), output_field=models.BooleanField()
|
|
|
|
),
|
|
|
|
name='%(app_label)s_price_lt_1000_raw',
|
|
|
|
),
|
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.expressions.ExpressionWrapper(
|
|
|
|
models.Q(price__gt=500) | models.Q(price__lt=500),
|
|
|
|
output_field=models.BooleanField()
|
|
|
|
),
|
|
|
|
name='%(app_label)s_price_neq_500_wrap',
|
|
|
|
),
|
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'),
|
2020-04-17 13:45:52 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class UniqueConstraintConditionProduct(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
color = models.CharField(max_length=32, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
required_db_features = {'supports_partial_indexes'}
|
|
|
|
constraints = [
|
2018-12-28 03:21:59 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
2018-08-27 10:25:06 +08:00
|
|
|
class UniqueConstraintDeferrable(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
shelf = models.CharField(max_length=31)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
required_db_features = {
|
|
|
|
'supports_deferrable_unique_constraints',
|
|
|
|
}
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=['name'],
|
|
|
|
name='name_init_deferred_uniq',
|
|
|
|
deferrable=models.Deferrable.DEFERRED,
|
|
|
|
),
|
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=['shelf'],
|
|
|
|
name='sheld_init_immediate_uniq',
|
|
|
|
deferrable=models.Deferrable.IMMEDIATE,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-10-31 20:33:53 +08:00
|
|
|
class UniqueConstraintInclude(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
color = models.CharField(max_length=32, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
required_db_features = {
|
|
|
|
'supports_table_check_constraints',
|
2020-06-03 12:44:05 +08:00
|
|
|
'supports_covering_indexes',
|
2019-10-31 20:33:53 +08:00
|
|
|
}
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=['name'],
|
|
|
|
name='name_include_color_uniq',
|
|
|
|
include=['color'],
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
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
|