Updated the tests.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8268 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
dc14b29fb3
commit
c763f26173
|
@ -282,6 +282,7 @@ DoesNotExist: Restaurant matching query does not exist.
|
||||||
# The update() command can update fields in parent and child classes at once
|
# The update() command can update fields in parent and child classes at once
|
||||||
# (although it executed multiple SQL queries to do so).
|
# (although it executed multiple SQL queries to do so).
|
||||||
>>> Restaurant.objects.filter(serves_hot_dogs=True, name__contains='D').update(name='Demon Puppies', serves_hot_dogs=False)
|
>>> Restaurant.objects.filter(serves_hot_dogs=True, name__contains='D').update(name='Demon Puppies', serves_hot_dogs=False)
|
||||||
|
1
|
||||||
>>> r1 = Restaurant.objects.get(pk=r.pk)
|
>>> r1 = Restaurant.objects.get(pk=r.pk)
|
||||||
>>> r1.serves_hot_dogs == False
|
>>> r1.serves_hot_dogs == False
|
||||||
True
|
True
|
||||||
|
|
|
@ -24,20 +24,21 @@ class RelatedPoint(models.Model):
|
||||||
__test__ = {'API_TESTS': """
|
__test__ = {'API_TESTS': """
|
||||||
>>> DataPoint(name="d0", value="apple").save()
|
>>> DataPoint(name="d0", value="apple").save()
|
||||||
>>> DataPoint(name="d2", value="banana").save()
|
>>> DataPoint(name="d2", value="banana").save()
|
||||||
>>> d3 = DataPoint(name="d3", value="banana")
|
>>> d3 = DataPoint.objects.create(name="d3", value="banana")
|
||||||
>>> d3.save()
|
|
||||||
>>> RelatedPoint(name="r1", data=d3).save()
|
>>> RelatedPoint(name="r1", data=d3).save()
|
||||||
|
|
||||||
Objects are updated by first filtering the candidates into a queryset and then
|
Objects are updated by first filtering the candidates into a queryset and then
|
||||||
calling the update() method. It executes immediately and returns nothing.
|
calling the update() method. It executes immediately and returns nothing.
|
||||||
|
|
||||||
>>> DataPoint.objects.filter(value="apple").update(name="d1")
|
>>> DataPoint.objects.filter(value="apple").update(name="d1")
|
||||||
|
1
|
||||||
>>> DataPoint.objects.filter(value="apple")
|
>>> DataPoint.objects.filter(value="apple")
|
||||||
[<DataPoint: d1>]
|
[<DataPoint: d1>]
|
||||||
|
|
||||||
We can update multiple objects at once.
|
We can update multiple objects at once.
|
||||||
|
|
||||||
>>> DataPoint.objects.filter(value="banana").update(value="pineapple")
|
>>> DataPoint.objects.filter(value="banana").update(value="pineapple")
|
||||||
|
2
|
||||||
>>> DataPoint.objects.get(name="d2").value
|
>>> DataPoint.objects.get(name="d2").value
|
||||||
u'pineapple'
|
u'pineapple'
|
||||||
|
|
||||||
|
@ -46,12 +47,14 @@ referred to, not anything inside the related object.
|
||||||
|
|
||||||
>>> d = DataPoint.objects.get(name="d1")
|
>>> d = DataPoint.objects.get(name="d1")
|
||||||
>>> RelatedPoint.objects.filter(name="r1").update(data=d)
|
>>> RelatedPoint.objects.filter(name="r1").update(data=d)
|
||||||
|
1
|
||||||
>>> RelatedPoint.objects.filter(data__name="d1")
|
>>> RelatedPoint.objects.filter(data__name="d1")
|
||||||
[<RelatedPoint: r1>]
|
[<RelatedPoint: r1>]
|
||||||
|
|
||||||
Multiple fields can be updated at once
|
Multiple fields can be updated at once
|
||||||
|
|
||||||
>>> DataPoint.objects.filter(value="pineapple").update(value="fruit", another_value="peaches")
|
>>> DataPoint.objects.filter(value="pineapple").update(value="fruit", another_value="peaches")
|
||||||
|
2
|
||||||
>>> d = DataPoint.objects.get(name="d2")
|
>>> d = DataPoint.objects.get(name="d2")
|
||||||
>>> d.value, d.another_value
|
>>> d.value, d.another_value
|
||||||
(u'fruit', u'peaches')
|
(u'fruit', u'peaches')
|
||||||
|
@ -60,6 +63,7 @@ In the rare case you want to update every instance of a model, update() is also
|
||||||
a manager method.
|
a manager method.
|
||||||
|
|
||||||
>>> DataPoint.objects.update(value='thing')
|
>>> DataPoint.objects.update(value='thing')
|
||||||
|
3
|
||||||
>>> DataPoint.objects.values('value').distinct()
|
>>> DataPoint.objects.values('value').distinct()
|
||||||
[{'value': u'thing'}]
|
[{'value': u'thing'}]
|
||||||
|
|
||||||
|
|
|
@ -767,6 +767,7 @@ Updates that are filtered on the model being updated are somewhat tricky to get
|
||||||
in MySQL. This exercises that case.
|
in MySQL. This exercises that case.
|
||||||
>>> mm = ManagedModel.objects.create(data='mm1', tag=t1, public=True)
|
>>> mm = ManagedModel.objects.create(data='mm1', tag=t1, public=True)
|
||||||
>>> ManagedModel.objects.update(data='mm')
|
>>> ManagedModel.objects.update(data='mm')
|
||||||
|
1
|
||||||
|
|
||||||
A values() or values_list() query across joined models must use outer joins
|
A values() or values_list() query across joined models must use outer joins
|
||||||
appropriately.
|
appropriately.
|
||||||
|
|
Loading…
Reference in New Issue