Using Django Check Constraints for the Sum of Percentage Fields

栏目: IT技术 · 发布时间: 6年前

内容简介:I previously covered using Django’sImagine a book application where we want to track books our percentage progress in three categories:We could use a Django model with one field for each of those three categories:
Using Django Check Constraints for the Sum of Percentage Fields

I previously covered using Django’s CheckConstraint class to ensure a field with choices is constrained to only valid values . Here’s another use case, based on an application I worked on. It uses a check constraint to ensure a set of fields, representing percentages, always sum up to 100.

Imagine a book application where we want to track books our percentage progress in three categories:

  • Pages we’ve read
  • Pages we have left to read
  • Pages we have deliberately chosen to ignore

We could use a Django model with one field for each of those three categories:

from django.db import models


class Book(models.Model):
    percent_read = models.PositiveIntegerField()
    percent_unread = models.PositiveIntegerField()
    percent_ignored = models.PositiveIntegerField()

    def __str__(self):
        return f"{self.id} - {self.percent_read}% read"

Using PositiveIntegerField means no field contains a number less than 0. That’s a good start, but the fields can still store numbers greater than 100, or their sum might be less or more than 100.

Using a check constraint, we can enforce such constraints, telling the database to prevent storage of bad data. In this case, we only need to enforce that their sum is 100 to automatically bound the individual fields between 0 and 100.

We add such a constraint in the model’s Meta.constraints :

from django.db import models


class Book(models.Model):
    percent_read = models.PositiveIntegerField()
    percent_unread = models.PositiveIntegerField()
    percent_ignored = models.PositiveIntegerField()

    def __str__(self):
        return f"{self.id} - {self.percent_read}% read"

    class Meta:
        constraints = [
            models.CheckConstraint(
                check=models.Q(
                    percent_read=(
                        100
                        - models.F("percent_unread")
                        - models.F("percent_ignored")
                    )
                ),
                name="percentages_sum_100",
            )
        ]

Notes:

  • We always have to wrap the expression in a Q() object . This represents a filter, and takes the same syntax as arguments to objects.filter() .

  • We use F() objects to refer to the fields in our model.

  • We combine the F() objects using Python’s mathematical operators. This doesn’t execute the operation but builds a representation that the database will execute after we migrate.

  • We have to write the expression with percent_read on the left hand side. That is, percent_read == 100 - percent_unread - percent_ignored , rather than the clearer percent_read + percent_unread + percent_ignored == 100 . This is due to limitations in Django’s F() object that we might remove in a future version (e.g. PR #12041 ).

Running makemigrations gives us a migration like so:

from django.db import migrations, models
import django.db.models.expressions


class Migration(migrations.Migration):

    dependencies = [
        ("core", "0001_initial"),
    ]

    operations = [
        migrations.AddConstraint(
            model_name="book",
            constraint=models.CheckConstraint(
                check=models.Q(
                    percent_read=django.db.models.expressions.CombinedExpression(
                        django.db.models.expressions.CombinedExpression(
                            django.db.models.expressions.Value(100),
                            "-",
                            django.db.models.expressions.F("percent_unread"),
                        ),
                        "-",
                        django.db.models.expressions.F("percent_ignored"),
                    )
                ),
                name="percentages_sum_100",
            ),
        ),
    ]

This looks like our model definition. The main difference is that the migrations framework has swapped the F() objects for the constructed CombinedExpression objects.

Running sqlmigrate reveals the SQL it will execute:

$ python manage.py sqlmigrate core 0002
--
-- Create constraint percentages_sum_100 on model book
--
ALTER TABLE `core_book` ADD CONSTRAINT `percentages_sum_100` CHECK (
    `percent_read` = (
        (100 - `core_book`.`percent_unread`)
        - `core_book`.`percent_ignored`
    )
);

We can apply this migration using migrate , but only if all the data in the table already fits the constraint.

If it doesn’t, the database will raise an IntegrityError :

$ python manage.py migrate core 0002
Operations to perform:
  Target specific migration: 0002_percentages_sum_100, from core
Running migrations:
  Applying core.0002_percentages_sum_100...Traceback (most recent call last):
...
django.db.utils.IntegrityError: (4025, 'CONSTRAINT `percentages_sum_100` failed for `testapp`.`core_book`')

After successfully migrating, the database will raise an IntegrityError like this for inserts or updates of bad data.

As I wrote inmy past post, constraints aren’t surfaced as nice error messages in forms. The best solution right now is to reimplement the constraint logic in Python in our form.

In this case we need to implement a Form.clean() method , since our validation is for more than one field:

from django import forms

from core.models import Book


class BookForm(forms.ModelForm):
    class Meta:
        model = Book
        fields = ["percent_read", "percent_unread", "percent_ignored"]

    def clean(self):
        cleaned_data = super().clean()

        try:
            percentages_sum = (
                cleaned_data["percent_read"]
                + cleaned_data["percent_unread"]
                + cleaned_data["percent_ignored"]
            )
        except KeyError:
            pass
        else:
            if percentages_sum != 100:
                self.add_error(
                    None,
                    (
                        f"Percentages must add up to 100%, they currently add"
                        + f" up to {percentages_sum}%"
                    )
                )

        return cleaned_data

Notes:

  • We use try/except KeyError/else to calculate the sum. The data dict can raise a KeyError for fields that the user didn’t provide. I covered using else like this in “Limit Your Try Clauses in Python” .

  • We use self.add_error(None, msg) to add a non-field error. This is better than raise ValidationError since it allows further steps we might add to clean() to run as well.

A quick test of the form shows the clean method working:

$ python manage.py shell
...
In [1]: from core.forms import BookForm

In [2]: BookForm({'percent_unread': 12, 'percent_read': 12, 'percent_ignored': 12}).errors
Out[2]: {'__all__': ['Percentages must add up to 100%, they currently add up to 36%']}

In [3]: BookForm({'percent_unread': 33, 'percent_read': 33, 'percent_ignored': 34}).errors
Out[3]: {}

Great!

Fin

During the writing of this blog post, I must admit I found a bug in Django, #31197 . Adding this constraint won’t cleanly migrate on SQLite, on Django 2.2 or 3.0, but it’s fixed for 3.1 and I wrote a workaround on the ticket. Thanks to Simon Charette for writing the fix and Mariusz Felisiak for adding a test.

I hope this post helps you use check constraints more,

—Adam

Interested in Django or Python training?I'm taking bookingsfor workshops.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

奔跑吧 Linux内核

奔跑吧 Linux内核

张天飞 / 人民邮电出版社 / 2017-9-1 / CNY 158.00

本书内容基于Linux4.x内核,主要选取了Linux内核中比较基本和常用的内存管理、进程管理、并发与同步,以及中断管理这4个内核模块进行讲述。全书共分为6章,依次介绍了ARM体系结构、Linux内存管理、进程调度管理、并发与同步、中断管理、内核调试技巧等内容。本书的每节内容都是一个Linux内核的话题或者技术点,读者可以根据每小节前的问题进行思考,进而围绕问题进行内核源代码的分析。 本书内......一起来看看 《奔跑吧 Linux内核》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具