Code Monkey home page Code Monkey logo

dj-check-constraint-validation's Introduction

dj-check-constraint-validation

From the constraints reference in the django documentation,

Validation of Constraints

In general constraints are not checked during full_clean(), and do not raise ValidationErrors. Rather you’ll get a database integrity error on save(). UniqueConstraints without a condition (i.e. non-partial unique constraints) are different in this regard, in that they leverage the existing validate_unique() logic, and thus enable two-stage validation. In addition to IntegrityError on save(), ValidationError is also raised during model validation when the UniqueConstraint is violated.

dj-check-constraint-validation.CheckConstraintMixin will programmatically generate form validation from the Q objects specified in the CheckConstraint.check. This leads us to define the check constraint on the model once without having to define similar logic again on the form.

Installation

pip install dj-check-constraint-validation

Quickstart

from django.db import models
from django import forms
from django.db.models import CheckConstraint, F, Q

from dj_check_constraint_validation import CheckConstraintsMixin

class Dog(models.Model):
    name = models.CharField(max_length=32)
    nick_name = models.CharField(max_length=32)

    class Meta:
        constraints = [
            CheckConstraint(check=~Q(name=F("nick_name")), name="name-nick-name-check"),
        ]

class DogForm(CheckConstraintsMixin, forms.ModelForm):
    class Meta:
        model = Dog
        fields = ["name", "nick_name"]

Supported constraints

The code to evaluate Q objects in python is in the eval_q function. This function currently supports the following:

type example
Q CheckConstraint(check=~Q(name="Bad name"), ...)
Q with F CheckConstraint(check=~Q(name=F("nick_name")), ...)
Q with CombinedExpression CheckConstraint(check=~Q(balance=F("price") - F("discount"), ...)

There is no support for field lookups

dj-check-constraint-validation's People

Contributors

massover avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.