Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2010-04-03 19:45:31 +08:00
|
|
|
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)
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
__test__ = {'API_TESTS': """
|
|
|
|
>>> DataPoint(name="d0", value="apple").save()
|
|
|
|
>>> DataPoint(name="d2", value="banana").save()
|
2008-08-10 01:19:44 +08:00
|
|
|
>>> d3 = DataPoint.objects.create(name="d3", value="banana")
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
>>> 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")
|
2008-08-10 01:19:44 +08:00
|
|
|
1
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
>>> DataPoint.objects.filter(value="apple")
|
|
|
|
[<DataPoint: d1>]
|
|
|
|
|
|
|
|
We can update multiple objects at once.
|
|
|
|
|
|
|
|
>>> DataPoint.objects.filter(value="banana").update(value="pineapple")
|
2008-08-10 01:19:44 +08:00
|
|
|
2
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
>>> 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)
|
2008-08-10 01:19:44 +08:00
|
|
|
1
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
>>> 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")
|
2008-08-10 01:19:44 +08:00
|
|
|
2
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
>>> 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')
|
2008-08-10 01:19:44 +08:00
|
|
|
3
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
>>> DataPoint.objects.values('value').distinct()
|
|
|
|
[{'value': u'thing'}]
|
|
|
|
|
2008-06-10 00:17:54 +08:00
|
|
|
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.
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
"""
|
|
|
|
}
|