2016-11-05 13:12:12 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
class Product(models.Model):
|
2018-08-06 02:30:44 +00:00
|
|
|
price = models.IntegerField(null=True)
|
|
|
|
discounted_price = models.IntegerField(null=True)
|
2020-07-27 04:39:02 +00:00
|
|
|
unit = models.CharField(max_length=15, null=True)
|
2016-11-05 13:12:12 +00:00
|
|
|
|
|
|
|
class Meta:
|
2019-08-10 06:41:18 +00:00
|
|
|
required_db_features = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"supports_table_check_constraints",
|
2019-08-10 06:41:18 +00:00
|
|
|
}
|
2016-11-05 13:12:12 +00:00
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
2022-02-03 19:24:19 +00:00
|
|
|
check=models.Q(price__gt=models.F("discounted_price")),
|
|
|
|
name="price_gt_discounted_price",
|
2018-08-06 02:15:10 +00:00
|
|
|
),
|
2019-07-05 12:15:41 +00:00
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.Q(price__gt=0),
|
2022-02-03 19:24:19 +00:00
|
|
|
name="%(app_label)s_%(class)s_price_gt_0",
|
2019-07-05 12:15:41 +00:00
|
|
|
),
|
2020-07-27 04:39:02 +00:00
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.Q(
|
2022-02-03 19:24:19 +00:00
|
|
|
models.Q(unit__isnull=True) | models.Q(unit__in=["μg/mL", "ng/mL"])
|
2020-07-27 04:39:02 +00:00
|
|
|
),
|
2022-02-03 19:24:19 +00:00
|
|
|
name="unicode_unit_list",
|
2020-07-27 04:39:02 +00:00
|
|
|
),
|
2019-08-10 06:41:18 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class UniqueConstraintProduct(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
color = models.CharField(max_length=32, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
2022-02-03 19:24:19 +00:00
|
|
|
models.UniqueConstraint(fields=["name", "color"], name="name_color_uniq"),
|
2020-04-17 05:45:52 +00:00
|
|
|
]
|
2022-01-31 15:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChildUniqueConstraintProduct(UniqueConstraintProduct):
|
|
|
|
pass
|
2020-04-17 05:45:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UniqueConstraintConditionProduct(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
color = models.CharField(max_length=32, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
2022-02-03 19:24:19 +00:00
|
|
|
required_db_features = {"supports_partial_indexes"}
|
2020-04-17 05:45:52 +00:00
|
|
|
constraints = [
|
2018-12-27 19:21:59 +00:00
|
|
|
models.UniqueConstraint(
|
2022-02-03 19:24:19 +00:00
|
|
|
fields=["name"],
|
|
|
|
name="name_without_color_uniq",
|
2018-12-27 19:21:59 +00:00
|
|
|
condition=models.Q(color__isnull=True),
|
|
|
|
),
|
2016-11-05 13:12:12 +00:00
|
|
|
]
|
2019-07-05 12:15:41 +00:00
|
|
|
|
|
|
|
|
2018-08-27 02:25:06 +00:00
|
|
|
class UniqueConstraintDeferrable(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
shelf = models.CharField(max_length=31)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
required_db_features = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"supports_deferrable_unique_constraints",
|
2018-08-27 02:25:06 +00:00
|
|
|
}
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(
|
2022-02-03 19:24:19 +00:00
|
|
|
fields=["name"],
|
|
|
|
name="name_init_deferred_uniq",
|
2018-08-27 02:25:06 +00:00
|
|
|
deferrable=models.Deferrable.DEFERRED,
|
|
|
|
),
|
|
|
|
models.UniqueConstraint(
|
2022-02-03 19:24:19 +00:00
|
|
|
fields=["shelf"],
|
|
|
|
name="sheld_init_immediate_uniq",
|
2018-08-27 02:25:06 +00:00
|
|
|
deferrable=models.Deferrable.IMMEDIATE,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-10-31 12:33:53 +00:00
|
|
|
class UniqueConstraintInclude(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
color = models.CharField(max_length=32, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
required_db_features = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"supports_table_check_constraints",
|
|
|
|
"supports_covering_indexes",
|
2019-10-31 12:33:53 +00:00
|
|
|
}
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(
|
2022-02-03 19:24:19 +00:00
|
|
|
fields=["name"],
|
|
|
|
name="name_include_color_uniq",
|
|
|
|
include=["color"],
|
2019-10-31 12:33:53 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-07-05 12:15:41 +00:00
|
|
|
class AbstractModel(models.Model):
|
|
|
|
age = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2019-08-10 06:41:18 +00:00
|
|
|
required_db_features = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"supports_table_check_constraints",
|
2019-08-10 06:41:18 +00:00
|
|
|
}
|
2019-07-05 12:15:41 +00:00
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.Q(age__gte=18),
|
2022-02-03 19:24:19 +00:00
|
|
|
name="%(app_label)s_%(class)s_adult",
|
2019-07-05 12:15:41 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ChildModel(AbstractModel):
|
|
|
|
pass
|