内容简介: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:
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 toobjects.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 clearerpercent_read + percent_unread + percent_ignored == 100
. This is due to limitations in Django’sF()
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 aKeyError
for fields that the user didn’t provide. I covered usingelse
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 thanraise ValidationError
since it allows further steps we might add toclean()
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.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
编程珠玑(第2版•修订版)
[美] Jon Bentley 乔恩•本特利 / 黄倩、钱丽艳 / 人民邮电出版社 / 2014-12 / 39
历史上最伟大的计算机科学著作之一 融深邃思想、实战技术与趣味轶事于一炉的奇书 带你真正领略计算机科学之美 多年以来,当程序员们推选出最心爱的计算机图书时,《编程珠玑》总是位于前列。正如自然界里珍珠出自细沙对牡蛎的磨砺,计算机科学大师Jon Bentley以其独有的洞察力和创造力,从磨砺程序员的实际问题中凝结出一篇篇不朽的编程“珠玑”,成为世界计算机界名刊《ACM通讯》历史上最受欢......一起来看看 《编程珠玑(第2版•修订版)》 这本书的介绍吧!
JS 压缩/解压工具
在线压缩/解压 JS 代码
随机密码生成器
多种字符组合密码