#11527 closed (fixed)
Documentation and testing for F() assignment to model attributes
Reported by: | Zachary Voase | Owned by: | Zachary Voase |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Add explicit documentation and regression tests for the use of django.db.models.F()
-expressions when assigning directly to attributes on model instances (as opposed to just using them in batch update()
calls).
The pattern goes something like this:
>>> from django.db.models import F >>> from myapp.models import MyModel >>> obj = MyModel(count=1) >>> obj.save() >>> obj.count 1 >>> obj.count = F('count') + 2 >>> obj.save() >>> obj = MyModel.objects.get(pk=obj.pk) # Necessary to reload here, as the `count` attribute will still be an Expression object. >>> obj.count 3
This should not work on records which do not already exist, since F()
-expressions only work in UPDATE
queries.
It's also important for this to work with any pre-save and post-save signal handlers defined in any of the django.contrib apps.
Attachments (1)
Change History (8)
comment:1 by , 15 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 15 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
comment:3 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 15 years ago
Has patch: | set |
---|---|
Needs documentation: | unset |
Needs tests: | unset |
I've tried to use my initiative in figuring out where the documentation should be. The tests may not pass on other DBMSes, but I don't have an installation of MySQL, Oracle or PostgreSQL on which to run them.
If you want to run the tests, just run python tests/runtests.py --settings=project.settings expressions
from the root of the SVN repository.
comment:5 by , 15 years ago
Interesting... your unit test has highlighted a bug - you should be able to assign foreign key values. This isn't a critical bug, and it's something we can add later without difficulty; I've opened ticket #11541 so that the issue isn't forgotten.
comment:6 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Before anyone gets too enthusiastic in triaging this off the milestone - This came up on django-dev, and I asked Zachary to post a ticket for v1.1, since it's docs and tests for a v1.1 feature. We can easily defer this if we don't get a patch before v1.1 final, but in the meantime, this is a placeholder.