Author: russellm
Date: 2011-08-22 22:12:31 -0700 (Mon, 22 Aug 2011)
New Revision: 16663

Modified:
   django/trunk/django/db/models/options.py
   django/trunk/tests/modeltests/invalid_models/invalid_models/models.py
Log:
Fixed #16299 -- Ensure that unicode strings can be used to identify classes in 
ForeignKey and ManyToManyFields. Unicode strings aren't actually legal as class 
names, but this is an issue if you use from __future__ import unicode_literals 
in your models.py file. Thanks to Martijn Bastiaan for the report, and Anthony 
Briggs for the final patch.

Modified: django/trunk/django/db/models/options.py
===================================================================
--- django/trunk/django/db/models/options.py    2011-08-23 04:41:56 UTC (rev 
16662)
+++ django/trunk/django/db/models/options.py    2011-08-23 05:12:31 UTC (rev 
16663)
@@ -385,7 +385,7 @@
                     cache[obj] = model
         for klass in get_models(include_auto_created=True, 
only_installed=False):
             for f in klass._meta.local_fields:
-                if f.rel and not isinstance(f.rel.to, str) and self == 
f.rel.to._meta:
+                if f.rel and not isinstance(f.rel.to, basestring) and self == 
f.rel.to._meta:
                     cache[RelatedObject(f.rel.to, klass, f)] = None
         self._related_objects_cache = cache
 
@@ -422,7 +422,7 @@
                     cache[obj] = model
         for klass in get_models(only_installed=False):
             for f in klass._meta.local_many_to_many:
-                if f.rel and not isinstance(f.rel.to, str) and self == 
f.rel.to._meta:
+                if f.rel and not isinstance(f.rel.to, basestring) and self == 
f.rel.to._meta:
                     cache[RelatedObject(f.rel.to, klass, f)] = None
         if app_cache_ready():
             self._related_many_to_many_cache = cache

Modified: django/trunk/tests/modeltests/invalid_models/invalid_models/models.py
===================================================================
--- django/trunk/tests/modeltests/invalid_models/invalid_models/models.py       
2011-08-23 04:41:56 UTC (rev 16662)
+++ django/trunk/tests/modeltests/invalid_models/invalid_models/models.py       
2011-08-23 05:12:31 UTC (rev 16663)
@@ -1,3 +1,4 @@
+#encoding=utf-8
 """
 26. Invalid models
 
@@ -218,6 +219,16 @@
 class InvalidSetDefault(models.Model):
     fk = models.ForeignKey('self', on_delete=models.SET_DEFAULT)
 
+class UnicodeForeignKeys(models.Model):
+    """Foreign keys which can translate to ascii should be OK, but fail if 
they're not."""
+    good = models.ForeignKey(u'FKTarget')
+    also_good = models.ManyToManyField(u'FKTarget', related_name='unicode2')
+
+    # In Python 3 this should become legal, but currently causes unicode errors
+    # when adding the errors in core/management/validation.py
+    #bad = models.ForeignKey(u'★')
+
+
 model_errors = """invalid_models.fielderrors: "charfield": CharFields require 
a "max_length" attribute that is a positive integer.
 invalid_models.fielderrors: "charfield2": CharFields require a "max_length" 
attribute that is a positive integer.
 invalid_models.fielderrors: "charfield3": CharFields require a "max_length" 
attribute that is a positive integer.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to