2010-01-05 03:56:19 +00:00
|
|
|
from datetime import datetime
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db import models
|
2021-12-04 20:03:38 +00:00
|
|
|
from django.db.models.functions import Lower
|
2010-01-05 03:56:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_answer_to_universe(value):
|
|
|
|
if value != 42:
|
|
|
|
raise ValidationError(
|
|
|
|
"This is not the answer to life, universe and everything!", code="not42"
|
|
|
|
)
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
class ModelToValidate(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
created = models.DateTimeField(default=datetime.now)
|
2010-01-29 18:38:28 +00:00
|
|
|
number = models.IntegerField(db_column="number_val")
|
2015-07-22 14:43:21 +00:00
|
|
|
parent = models.ForeignKey(
|
|
|
|
"self",
|
|
|
|
models.SET_NULL,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
limit_choices_to={"number": 10},
|
|
|
|
)
|
2010-01-05 03:56:19 +00:00
|
|
|
email = models.EmailField(blank=True)
|
2015-07-22 14:43:21 +00:00
|
|
|
ufm = models.ForeignKey(
|
|
|
|
"UniqueFieldsModel",
|
|
|
|
models.SET_NULL,
|
|
|
|
to_field="unique_charfield",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
)
|
2010-01-05 03:56:19 +00:00
|
|
|
url = models.URLField(blank=True)
|
|
|
|
f_with_custom_validator = models.IntegerField(
|
|
|
|
blank=True, null=True, validators=[validate_answer_to_universe]
|
|
|
|
)
|
2016-04-22 00:18:43 +00:00
|
|
|
f_with_iterable_of_validators = models.IntegerField(
|
|
|
|
blank=True, null=True, validators=(validate_answer_to_universe,)
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2013-06-13 16:37:08 +00:00
|
|
|
slug = models.SlugField(blank=True)
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2010-01-12 02:29:45 +00:00
|
|
|
def clean(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().clean()
|
2010-01-05 03:56:19 +00:00
|
|
|
if self.number == 11:
|
|
|
|
raise ValidationError("Invalid number supplied!")
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
class UniqueFieldsModel(models.Model):
|
|
|
|
unique_charfield = models.CharField(max_length=100, unique=True)
|
2024-08-05 06:22:29 +00:00
|
|
|
unique_integerfield = models.IntegerField(unique=True, db_default=42)
|
2010-01-05 03:56:19 +00:00
|
|
|
non_unique_field = models.IntegerField()
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
class CustomPKModel(models.Model):
|
|
|
|
my_pk_field = models.CharField(max_length=100, primary_key=True)
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
class UniqueTogetherModel(models.Model):
|
|
|
|
cfield = models.CharField(max_length=100)
|
|
|
|
ifield = models.IntegerField()
|
|
|
|
efield = models.EmailField()
|
|
|
|
|
|
|
|
class Meta:
|
2010-02-10 00:49:33 +00:00
|
|
|
unique_together = (
|
2022-02-03 19:24:19 +00:00
|
|
|
(
|
2010-02-10 00:49:33 +00:00
|
|
|
"ifield",
|
|
|
|
"cfield",
|
|
|
|
),
|
|
|
|
["ifield", "efield"],
|
|
|
|
)
|
2010-01-05 03:56:19 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
class UniqueForDateModel(models.Model):
|
|
|
|
start_date = models.DateField()
|
|
|
|
end_date = models.DateTimeField()
|
|
|
|
count = models.IntegerField(
|
|
|
|
unique_for_date="start_date", unique_for_year="end_date"
|
|
|
|
)
|
|
|
|
order = models.IntegerField(unique_for_month="end_date")
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-01-05 03:56:19 +00:00
|
|
|
class CustomMessagesModel(models.Model):
|
2013-10-22 13:31:43 +00:00
|
|
|
other = models.IntegerField(blank=True, null=True)
|
2016-04-08 02:04:45 +00:00
|
|
|
number = models.IntegerField(
|
|
|
|
db_column="number_val",
|
2010-01-05 03:56:19 +00:00
|
|
|
error_messages={"null": "NULL", "not42": "AAARGH", "not_equal": "%s != me"},
|
|
|
|
validators=[validate_answer_to_universe],
|
|
|
|
)
|
2010-01-12 02:29:45 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2020-06-25 08:28:52 +00:00
|
|
|
class AuthorManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
return qs.filter(archived=False)
|
|
|
|
|
|
|
|
|
2010-01-12 02:29:45 +00:00
|
|
|
class Author(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2020-06-25 08:28:52 +00:00
|
|
|
archived = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
objects = AuthorManager()
|
2010-01-12 02:29:45 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2010-01-12 02:29:45 +00:00
|
|
|
class Article(models.Model):
|
|
|
|
title = models.CharField(max_length=100)
|
2015-07-22 14:43:21 +00:00
|
|
|
author = models.ForeignKey(Author, models.CASCADE)
|
2010-01-12 02:29:45 +00:00
|
|
|
pub_date = models.DateTimeField(blank=True)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if self.pub_date is None:
|
|
|
|
self.pub_date = datetime.now()
|
2011-01-09 13:26:39 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2011-01-09 13:26:39 +00:00
|
|
|
class Post(models.Model):
|
|
|
|
title = models.CharField(max_length=50, unique_for_date="posted", blank=True)
|
|
|
|
slug = models.CharField(max_length=50, unique_for_year="posted", blank=True)
|
|
|
|
subtitle = models.CharField(max_length=50, unique_for_month="posted", blank=True)
|
|
|
|
posted = models.DateField()
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2011-01-09 13:26:39 +00:00
|
|
|
class FlexibleDatePost(models.Model):
|
|
|
|
title = models.CharField(max_length=50, unique_for_date="posted", blank=True)
|
|
|
|
slug = models.CharField(max_length=50, unique_for_year="posted", blank=True)
|
|
|
|
subtitle = models.CharField(max_length=50, unique_for_month="posted", blank=True)
|
|
|
|
posted = models.DateField(blank=True, null=True)
|
2011-06-09 15:05:13 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2011-06-09 15:05:13 +00:00
|
|
|
class UniqueErrorsModel(models.Model):
|
2012-06-07 16:08:47 +00:00
|
|
|
name = models.CharField(
|
|
|
|
max_length=100,
|
|
|
|
unique=True,
|
|
|
|
error_messages={"unique": "Custom unique name message."},
|
|
|
|
)
|
|
|
|
no = models.IntegerField(
|
|
|
|
unique=True, error_messages={"unique": "Custom unique number message."}
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2011-06-11 13:48:24 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2011-06-11 13:48:24 +00:00
|
|
|
class GenericIPAddressTestModel(models.Model):
|
2011-06-13 20:15:13 +00:00
|
|
|
generic_ip = models.GenericIPAddressField(blank=True, null=True, unique=True)
|
|
|
|
v4_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv4")
|
|
|
|
v6_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv6")
|
2016-04-08 02:04:45 +00:00
|
|
|
ip_verbose_name = models.GenericIPAddressField(
|
|
|
|
"IP Address Verbose", blank=True, null=True
|
|
|
|
)
|
2011-06-11 13:48:24 +00:00
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2011-06-13 17:48:02 +00:00
|
|
|
class GenericIPAddrUnpackUniqueTest(models.Model):
|
2013-05-28 19:11:41 +00:00
|
|
|
generic_v4unpack_ip = models.GenericIPAddressField(
|
|
|
|
null=True, blank=True, unique=True, unpack_ipv4=True
|
|
|
|
)
|
2021-12-04 20:03:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UniqueFuncConstraintModel(models.Model):
|
|
|
|
field = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
required_db_features = {"supports_expression_indexes"}
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(Lower("field"), name="func_lower_field_uq"),
|
|
|
|
]
|
2022-01-31 15:04:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Product(models.Model):
|
|
|
|
price = models.IntegerField(null=True)
|
|
|
|
discounted_price = models.IntegerField(null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
required_db_features = {
|
|
|
|
"supports_table_check_constraints",
|
|
|
|
}
|
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
2024-02-26 05:14:26 +00:00
|
|
|
condition=models.Q(price__gt=models.F("discounted_price")),
|
2022-01-31 15:04:13 +00:00
|
|
|
name="price_gt_discounted_price_validation",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ChildProduct(Product):
|
|
|
|
class Meta:
|
|
|
|
required_db_features = {
|
|
|
|
"supports_table_check_constraints",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class UniqueConstraintProduct(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
color = models.CharField(max_length=32)
|
|
|
|
rank = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=["name", "color"], name="name_color_uniq_validation"
|
|
|
|
),
|
|
|
|
models.UniqueConstraint(fields=["rank"], name="rank_uniq_validation"),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ChildUniqueConstraintProduct(UniqueConstraintProduct):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class UniqueConstraintConditionProduct(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
color = models.CharField(max_length=31, null=True, blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
required_db_features = {"supports_partial_indexes"}
|
|
|
|
constraints = [
|
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=["name"],
|
|
|
|
name="name_without_color_uniq_validation",
|
|
|
|
condition=models.Q(color__isnull=True),
|
|
|
|
),
|
|
|
|
]
|