File: models.py

package info (click to toggle)
python-django 1.2.3-3%2Bsqueeze15
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 29,720 kB
  • ctags: 21,538
  • sloc: python: 101,631; xml: 574; makefile: 149; sh: 121; sql: 7
file content (91 lines) | stat: -rw-r--r-- 2,490 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Tests for the update() queryset method that allows in-place, multi-object
updates.
"""

from django.db import models

class DataPoint(models.Model):
    name = models.CharField(max_length=20)
    value = models.CharField(max_length=20)
    another_value = models.CharField(max_length=20, blank=True)

    def __unicode__(self):
        return unicode(self.name)

class RelatedPoint(models.Model):
    name = models.CharField(max_length=20)
    data = models.ForeignKey(DataPoint)

    def __unicode__(self):
        return unicode(self.name)


class A(models.Model):
    x = models.IntegerField(default=10)

class B(models.Model):
    a = models.ForeignKey(A)
    y = models.IntegerField(default=10)

class C(models.Model):
    y = models.IntegerField(default=10)

class D(C):
    a = models.ForeignKey(A)

__test__ = {'API_TESTS': """
>>> DataPoint(name="d0", value="apple").save()
>>> DataPoint(name="d2", value="banana").save()
>>> d3 = DataPoint.objects.create(name="d3", value="banana")
>>> RelatedPoint(name="r1", data=d3).save()

Objects are updated by first filtering the candidates into a queryset and then
calling the update() method. It executes immediately and returns nothing.

>>> DataPoint.objects.filter(value="apple").update(name="d1")
1
>>> DataPoint.objects.filter(value="apple")
[<DataPoint: d1>]

We can update multiple objects at once.

>>> DataPoint.objects.filter(value="banana").update(value="pineapple")
2
>>> DataPoint.objects.get(name="d2").value
u'pineapple'

Foreign key fields can also be updated, although you can only update the object
referred to, not anything inside the related object.

>>> d = DataPoint.objects.get(name="d1")
>>> RelatedPoint.objects.filter(name="r1").update(data=d)
1
>>> RelatedPoint.objects.filter(data__name="d1")
[<RelatedPoint: r1>]

Multiple fields can be updated at once

>>> DataPoint.objects.filter(value="pineapple").update(value="fruit", another_value="peaches")
2
>>> d = DataPoint.objects.get(name="d2")
>>> d.value, d.another_value
(u'fruit', u'peaches')

In the rare case you want to update every instance of a model, update() is also
a manager method.

>>> DataPoint.objects.update(value='thing')
3
>>> DataPoint.objects.values('value').distinct()
[{'value': u'thing'}]

We do not support update on already sliced query sets.

>>> DataPoint.objects.all()[:2].update(another_value='another thing')
Traceback (most recent call last):
    ...
AssertionError: Cannot update a query once a slice has been taken.

"""
}