mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
db13bca60a
This constraint is similar to Meta.unique_together but also allows specifying a name. Co-authored-by: Ian Foote <python@ian.feete.org>
17 lines
499 B
Python
17 lines
499 B
Python
from django.db import models
|
|
|
|
|
|
class Product(models.Model):
|
|
name = models.CharField(max_length=255)
|
|
price = models.IntegerField(null=True)
|
|
discounted_price = models.IntegerField(null=True)
|
|
|
|
class Meta:
|
|
constraints = [
|
|
models.CheckConstraint(
|
|
check=models.Q(price__gt=models.F('discounted_price')),
|
|
name='price_gt_discounted_price',
|
|
),
|
|
models.UniqueConstraint(fields=['name'], name='unique_name'),
|
|
]
|