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: zacharyvoase Owned by: zacharyvoase
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:


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.count
>>> obj.count = F('count') + 2
>>> obj = MyModel.objects.get( # Necessary to reload here, as the `count` attribute will still be an Expression object.
>>> obj.count

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 zacharyvoase 7 years ago.
Initial patch. Tests pass on SQLite3.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 7 years ago by zacharyvoase

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to zacharyvoase
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 7 years ago by zacharyvoase

  • Needs documentation set
  • Needs tests set

comment:3 Changed 7 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

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 zacharyvoase

Initial patch. Tests pass on SQLite3.

comment:4 Changed 7 years ago by zacharyvoase

  • 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/ --settings=project.settings expressions from the root of the SVN repository.

comment:5 Changed 7 years ago by russellm

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 russellm

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 deleted

Milestone 1.1 deleted

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