内容简介: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_readon 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/elseto calculate the sum. The data dict can raise aKeyErrorfor fields that the user didn’t provide. I covered usingelselike 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 ValidationErrorsince 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.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
高性能JavaScript
【美】Nicholas C. Zakas(尼古拉斯.泽卡斯) / 丁琛 / 电子工业出版社 / 2015-8-1 / 65
如果你使用 JavaScript 构建交互丰富的 Web 应用,那么 JavaScript 代码可能是造成你的Web应用速度变慢的主要原因。《高性能JavaScript》揭示的技术和策略能帮助你在开发过程中消除性能瓶颈。你将会了解如何提升各方面的性能,包括代码的加载、运行、DOM 交互、页面生存周期等。雅虎的前端工程师 Nicholas C. Zakas 和其他五位 JavaScript 专家介绍......一起来看看 《高性能JavaScript》 这本书的介绍吧!