Questions tagged [django-constraints]

27 questions
8
votes
1 answer

Using Django's CheckConstraint with annotations

I have a Django model where each instance requires a unique identifier that is derived from three fields: class Example(Model): type = CharField(blank=False, null=False) # either 'A' or 'B' timestamp =…
3
votes
0 answers

What is best practice when using ValidationError and Constraint (new in Django 2.2)?

Edit: I am using PostgreSQL I have a model that has 4 phone number fields, and I run a ValidationError at the Model level using clean: class Person(models.Model): personal_phone_mobile = PhoneNumberField() personal_phone_direct =…
alias51
  • 8,178
  • 22
  • 94
  • 166
2
votes
1 answer

Unique together between field and model connected by ForeignKey django

I have two models. class Post(models.Model): title = models.CharField(max_length=150) class Attachment(models.Model): type = models.CharField(choices=AttachmentChoices.get_choices(), max_length=15) link = models.URLField() post =…
2
votes
0 answers

Django constraints for UniqueConstraint does not work

I have tried to implement constraints for UniqueConstraint for two foreign keys in the Django model. So far it has not been working as expected. Here below is the model definition : class AssetMember(models.Model): asset =…
london_utku
  • 1,070
  • 2
  • 16
  • 36
2
votes
1 answer

Django `UniqueConstraint` exception handled the same as 'unique=True'

When UniqueConstraint of a model is violated, an exception is raised. How can I make it to behave the same as violation of a field with unique=True? identifier = models.CharField("id", max_length=30, unique=True, blank=True, null=True,…
aaronn
  • 448
  • 1
  • 6
  • 16
2
votes
0 answers

Django Concat cannot be used in AddConstraint

I have a constraint like this CheckConstraint( check=Q(full_path__endswith=Concat(Value("/"), F("identifier"), Value("/"))) # | Q(full_path="/"), name="path_endswith_identifier_if_not_root", ), Makemigrations runs fine, but when I try…
Pinna_be
  • 4,517
  • 2
  • 18
  • 34
1
vote
1 answer

Django multi-table inheritance - make sure only one child exists (CheckConstraint)

How can I make sure that a parent object has only one child/type? class Property(...): class Meta: abstract = False class Flat(Property): pass class House(Property): pass class Land(Property): pass I want every property…
Milano
  • 18,048
  • 37
  • 153
  • 353
1
vote
1 answer

Django CheckConstraints to check start_date greater than or equal today

from django.db import models from django.db.models.functions import Now, TruncDay class Foo(models.Model): start_date = models.DateTimeField() end_date = models.DateTimeField() class Meta: constraints…
jujube
  • 11
  • 3
1
vote
1 answer

Constrain unique_together could have conflict with the unique_field in dajngo Class

I would like to know if there is any conflict using the constraint unique_together as well as the parameter unique=true in one of the fields that belong to the array of the unique_together. I can´t remove the parameter unique=true of my field…
1
vote
1 answer

Django Model Constraint Condition Using Field From Inherited Class - Is It Possible?

I'd like to use a field from a parent class as a constraint condition in a child class. models.py class ParentClass(object): ... is_public = models.BooleanField(default=False) class ChildClass(ParentClass): ... price =…
Jarad
  • 17,409
  • 19
  • 95
  • 154
1
vote
0 answers

Django constraint: allowing 1 field to only have a single combination with another field while still alowing historical data

I am trying to create a constraint on my Offer table, which has an offer_id (charfield) and a product_id (foreign key) where an offer_id can only be combined with 1 product. It is not allowed that an offer_id is combined with multiple products. I…
Mathijs
  • 177
  • 3
  • 18
1
vote
1 answer

Django: How to treat null as equal to everything for uniqueness constraints?

In the title I mean equal to any value, not just to other nulls. Let's say for the sake of the example that we have a household, with household members (people) and electronic devices. Some electronic devices are personal and belong to one person,…
talz
  • 1,004
  • 9
  • 22
1
vote
0 answers

Why is my model field out of scope in the constraints?

Models.py class Bug(models.Model): title = models.CharField(max_length=200) creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name="creator") lead = models.ForeignKey(User, default=None, blank=True,…
1
vote
1 answer

Django unit test constraints

So I have a problem in writing my unit tests for a Check and Unique constraint. Here are the their definition: # assert that a partner can have only one headquarter constraints = [ models.UniqueConstraint( fields=['partner', 'active'], …
davidm
  • 1,570
  • 11
  • 24
0
votes
1 answer

Django constraint for "field one not in field two for any row"?

Suppose I have a Django model with two fields one and two: class MyModel(models.Model): one = models.CharField(max_length=100, unique=True) two = models.CharField(max_length=100) unique=True means if I try to put in a row with one="one"…
dfrankow
  • 20,191
  • 41
  • 152
  • 214
1
2