Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixed #19816: pre-evaluate queryset on m2m set #769

Merged
merged 1 commit into from

2 participants

@oinopion

In ReverseManyRelatedObjectsDescriptor.set, evaluate possible
queryset to avoid problems when clear() would touch data this queryset
returns.

@oinopion oinopion Fixed #19816: pre-evaluate queryset on m2m set
In ReverseManyRelatedObjectsDescriptor.__set__, evaluate possible
queryset to avoid problems when clear() would touch data this queryset
returns.
3dddbc0
@HonzaKral HonzaKral merged commit 6cb6e11 into django:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 23, 2013
  1. @oinopion

    Fixed #19816: pre-evaluate queryset on m2m set

    oinopion authored
    In ReverseManyRelatedObjectsDescriptor.__set__, evaluate possible
    queryset to avoid problems when clear() would touch data this queryset
    returns.
This page is out of date. Refresh to see the latest.
View
3  django/db/models/fields/related.py
@@ -904,6 +904,9 @@ def __set__(self, instance, value):
raise AttributeError("Cannot set values on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead." % (opts.app_label, opts.object_name))
manager = self.__get__(instance)
+ # clear() can change expected output of 'value' queryset, we force evaluation
+ # of queryset before clear; ticket #19816
+ value = tuple(value)
manager.clear()
manager.add(*value)
View
15 tests/regressiontests/m2m_regress/tests.py
@@ -5,7 +5,7 @@
from django.utils import six
from .models import (SelfRefer, Tag, TagCollection, Entry, SelfReferChild,
- SelfReferChildSibling, Worksheet, RegressionModelSplit)
+ SelfReferChildSibling, Worksheet, RegressionModelSplit, Line)
class M2MRegressionTests(TestCase):
@@ -96,3 +96,16 @@ def test_m2m_abstract_split(self):
# causes a TypeError in add_lazy_relation
m1 = RegressionModelSplit(name='1')
m1.save()
+
+ def test_m2m_filter(self):
+ worksheet = Worksheet.objects.create(id=1)
+ line_hi = Line.objects.create(name="hi")
+ line_bye = Line.objects.create(name="bye")
+
+ worksheet.lines = [line_hi, line_bye]
+ hi = worksheet.lines.filter(name="hi")
+
+ worksheet.lines = hi
+ self.assertEquals(1, worksheet.lines.count())
+ self.assertEquals(1, hi.count())
+
Something went wrong with that request. Please try again.