Ticket #6045: 6045.solution_and_test.patch

File 6045.solution_and_test.patch, 2.6 KB (added by b.leskes@…, 13 years ago)

solution + test

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

     
    88from django.db.models.query import QuerySet
    99from django.db.models.query_utils import QueryWrapper
    1010from django.db.models.deletion import CASCADE
    11 from django.utils.encoding import smart_unicode
     11from django.utils.encoding import smart_unicode, smart_str
    1212from django.utils.translation import (ugettext_lazy as _, string_concat,
    1313    ungettext, ugettext)
    1414from django.utils.functional import curry
     
    10051005            assert not to._meta.abstract, "%s cannot define a relation with abstract class %s" % (self.__class__.__name__, to._meta.object_name)
    10061006        except AttributeError: # to._meta doesn't exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT
    10071007            assert isinstance(to, basestring), "%s(%r) is invalid. First parameter to ManyToManyField must be either a model, a model name, or the string %r" % (self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT)
     1008            to = smart_str(to,encoding='ascii') # normalize unicode strings to ascii as it is the only acceptable encoding for getattr
    10081009
    10091010        kwargs['verbose_name'] = kwargs.get('verbose_name', None)
    10101011        kwargs['rel'] = ManyToManyRel(to,
  • tests/modeltests/m2m_and_m2o/tests.py

     
    22from django.test import TestCase
    33
    44from models import Issue, User
     5from models import UnicodeReferenceModel
    56
    67
    78class RelatedObjectTests(TestCase):
     
    7374            ],
    7475            lambda i: i.num
    7576        )
     77
     78
     79
     80class RelatedObjectTests(TestCase):
     81    def test_m2m_with_unicode_reference(self):
     82        m1=UnicodeReferenceModel()
     83        m1.save()
     84        m2=UnicodeReferenceModel()
     85        m2.save() # create a PK so you can use m2m
     86        m2.others.add(m1) # used to cause an error (see ticket #6045)
     87        m2.save()
     88        for i in m2.others.all():
     89            pass # force retrieving.
  • tests/modeltests/m2m_and_m2o/models.py

     
    1919
    2020    class Meta:
    2121        ordering = ('num',)
     22
     23
     24class UnicodeReferenceModel(models.Model):
     25    others = models.ManyToManyField(u"UnicodeReferenceModel")
     26 No newline at end of file
Back to Top