Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#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: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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)

patch-v1.diff (3.6 KB) - added by Zachary Voase 7 years ago.
Initial patch. Tests pass on SQLite3.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 7 years ago by Zachary Voase

Needs documentation: unset
Needs tests: unset
Owner: changed from nobody to Zachary Voase
Patch needs improvement: unset
Status: newassigned

comment:2 Changed 7 years ago by Zachary Voase

Needs documentation: set
Needs tests: set

comment:3 Changed 7 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

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.

Changed 7 years ago by Zachary Voase

Attachment: patch-v1.diff added

Initial patch. Tests pass on SQLite3.

comment:4 Changed 7 years ago by Zachary Voase

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 Changed 7 years ago by Russell Keith-Magee

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 Changed 7 years ago by Russell Keith-Magee

Resolution: fixed
Status: assignedclosed

(In [11322]) Fixed #11527 -- Added unit tests and documentation for the use of F() expressions in single object updates. Thanks to Zachary Voase for the patch.

comment:7 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top