Ticket #19816: ticket19816.diff

File ticket19816.diff, 1.8 KB (added by ethlinn, 2 years ago)

Patch with tests added. Done by me and oinopion.

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index bea88ba..01b8a55 100644
    a b class ReverseManyRelatedObjectsDescriptor(object): 
    904904            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))
    905905
    906906        manager = self.__get__(instance)
     907        # clear() can change expected output of 'value' queryset, we force evaluation
     908        # of queryset before clear; ticket #19816
     909        value = tuple(value)
    907910        manager.clear()
    908911        manager.add(*value)
    909912
  • tests/regressiontests/m2m_regress/tests.py

    diff --git a/tests/regressiontests/m2m_regress/tests.py b/tests/regressiontests/m2m_regress/tests.py
    index 3dc1d24..90aab5e 100644
    a b from django.test import TestCase 
    55from django.utils import six
    66
    77from .models import (SelfRefer, Tag, TagCollection, Entry, SelfReferChild,
    8     SelfReferChildSibling, Worksheet, RegressionModelSplit)
     8    SelfReferChildSibling, Worksheet, RegressionModelSplit, Line)
    99
    1010
    1111class M2MRegressionTests(TestCase):
    class M2MRegressionTests(TestCase): 
    9696        # causes a TypeError in add_lazy_relation
    9797        m1 = RegressionModelSplit(name='1')
    9898        m1.save()
     99
     100    def test_m2m_filter(self):
     101        worksheet = Worksheet.objects.create(id=1)
     102        line_hi = Line.objects.create(name="hi")
     103        line_bye = Line.objects.create(name="bye")
     104
     105        worksheet.lines = [line_hi, line_bye]
     106        hi = worksheet.lines.filter(name="hi")
     107
     108        worksheet.lines = hi
     109        self.assertEquals(1, worksheet.lines.count())
     110        self.assertEquals(1, hi.count())
     111
Back to Top