~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to tests/modeltests/update/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2008-11-15 19:15:33 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081115191533-xbt1ut2xf4fvwtvc
Tags: 1.0.1-0ubuntu1
* New upstream release:
  - Bug fixes.

* The tests/ sub-directory appaers to have been dropped upstream, so pull
  our patch to workaround the tests and modify the rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Tests for the update() queryset method that allows in-place, multi-object
3
 
updates.
4
 
"""
5
 
 
6
 
from django.db import models
7
 
 
8
 
class DataPoint(models.Model):
9
 
    name = models.CharField(max_length=20)
10
 
    value = models.CharField(max_length=20)
11
 
    another_value = models.CharField(max_length=20, blank=True)
12
 
 
13
 
    def __unicode__(self):
14
 
        return unicode(self.name)
15
 
 
16
 
class RelatedPoint(models.Model):
17
 
    name = models.CharField(max_length=20)
18
 
    data = models.ForeignKey(DataPoint)
19
 
 
20
 
    def __unicode__(self):
21
 
        return unicode(self.name)
22
 
 
23
 
 
24
 
__test__ = {'API_TESTS': """
25
 
>>> DataPoint(name="d0", value="apple").save()
26
 
>>> DataPoint(name="d2", value="banana").save()
27
 
>>> d3 = DataPoint.objects.create(name="d3", value="banana")
28
 
>>> RelatedPoint(name="r1", data=d3).save()
29
 
 
30
 
Objects are updated by first filtering the candidates into a queryset and then
31
 
calling the update() method. It executes immediately and returns nothing.
32
 
 
33
 
>>> DataPoint.objects.filter(value="apple").update(name="d1")
34
 
1
35
 
>>> DataPoint.objects.filter(value="apple")
36
 
[<DataPoint: d1>]
37
 
 
38
 
We can update multiple objects at once.
39
 
 
40
 
>>> DataPoint.objects.filter(value="banana").update(value="pineapple")
41
 
2
42
 
>>> DataPoint.objects.get(name="d2").value
43
 
u'pineapple'
44
 
 
45
 
Foreign key fields can also be updated, although you can only update the object
46
 
referred to, not anything inside the related object.
47
 
 
48
 
>>> d = DataPoint.objects.get(name="d1")
49
 
>>> RelatedPoint.objects.filter(name="r1").update(data=d)
50
 
1
51
 
>>> RelatedPoint.objects.filter(data__name="d1")
52
 
[<RelatedPoint: r1>]
53
 
 
54
 
Multiple fields can be updated at once
55
 
 
56
 
>>> DataPoint.objects.filter(value="pineapple").update(value="fruit", another_value="peaches")
57
 
2
58
 
>>> d = DataPoint.objects.get(name="d2")
59
 
>>> d.value, d.another_value
60
 
(u'fruit', u'peaches')
61
 
 
62
 
In the rare case you want to update every instance of a model, update() is also
63
 
a manager method.
64
 
 
65
 
>>> DataPoint.objects.update(value='thing')
66
 
3
67
 
>>> DataPoint.objects.values('value').distinct()
68
 
[{'value': u'thing'}]
69
 
 
70
 
We do not support update on already sliced query sets.
71
 
 
72
 
>>> DataPoint.objects.all()[:2].update(another_value='another thing')
73
 
Traceback (most recent call last):
74
 
    ...
75
 
AssertionError: Cannot update a query once a slice has been taken.
76
 
 
77
 
"""
78
 
}