Opened 15 years ago

Closed 15 years ago

Last modified 13 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: 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)

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

Download all attachments as: .zip

Change History (8)

comment:1 by Zachary Voase, 15 years ago

Owner: changed from nobody to Zachary Voase
Status: newassigned

comment:2 by Zachary Voase, 15 years ago

Needs documentation: set
Needs tests: set

comment:3 by Russell Keith-Magee, 15 years ago

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.

by Zachary Voase, 15 years ago

Attachment: patch-v1.diff added

Initial patch. Tests pass on SQLite3.

comment:4 by Zachary Voase, 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 Russell Keith-Magee, 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 Russell Keith-Magee, 15 years ago

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 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

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