Django

Code

Ticket #11527 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

Documentation and testing for F() assignment to model attributes

Reported by: zacharyvoase Assigned to: zacharyvoase
Milestone: 1.1 Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

patch-v1.diff (3.6 kB) - added by zacharyvoase on 07/23/09 11:24:01.
Initial patch. Tests pass on SQLite3.

Change History

07/22/09 20:23:12 changed by zacharyvoase

  • owner changed from nobody to zacharyvoase.
  • needs_better_patch changed.
  • status changed from new to assigned.
  • needs_tests changed.
  • needs_docs changed.

07/22/09 20:23:27 changed by zacharyvoase

  • needs_docs set to 1.
  • needs_tests set to 1.

07/22/09 20:31:44 changed by russellm

  • 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.

07/23/09 11:24:01 changed by zacharyvoase

  • attachment patch-v1.diff added.

Initial patch. Tests pass on SQLite3.

07/23/09 11:27:48 changed by zacharyvoase

  • needs_docs deleted.
  • has_patch set to 1.
  • needs_tests deleted.

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.

07/24/09 08:36:53 changed 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.

07/24/09 08:38:37 changed by russellm

  • status changed from assigned to closed.
  • resolution set to fixed.

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


Add/Change #11527 (Documentation and testing for F() assignment to model attributes)




Change Properties
Action