Author: russellm
Date: 2009-11-10 09:21:12 -0600 (Tue, 10 Nov 2009)
New Revision: 11730
Modified:
django/trunk/django/db/models/fields/related.py
django/trunk/tests/regressiontests/many_to_one_regress/models.py
Log:
Fixed #12190 -- Corrected a regression in the ability to instantiate ForeignKeys outside of models. Thanks to jittat for the report.
Modified: django/trunk/django/db/models/fields/related.py
===================================================================
--- django/trunk/django/db/models/fields/related.py 2009-11-10 03:51:05 UTC (rev 11729)
+++ django/trunk/django/db/models/fields/related.py 2009-11-10 15:21:12 UTC (rev 11730)
@@ -695,6 +695,10 @@
assert isinstance(to, basestring), "%s(%r) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string %r" % (self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT)
else:
assert not to._meta.abstract, "%s cannot define a relation with abstract class %s" % (self.__class__.__name__, to._meta.object_name)
+ # For backwards compatibility purposes, we need to *try* and set
+ # the to_field during FK construction. It won't be guaranteed to
+ # be correct until contribute_to_class is called. Refs #12190.
+ to_field = to_field or (to._meta.pk and to._meta.pk.name)
kwargs['verbose_name'] = kwargs.get('verbose_name', None)
kwargs['rel'] = rel_class(to, to_field,
Modified: django/trunk/tests/regressiontests/many_to_one_regress/models.py
===================================================================
--- django/trunk/tests/regressiontests/many_to_one_regress/models.py 2009-11-10 03:51:05 UTC (rev 11729)
+++ django/trunk/tests/regressiontests/many_to_one_regress/models.py 2009-11-10 15:21:12 UTC (rev 11730)
@@ -167,4 +167,10 @@
...
ValueError: Cannot assign "<Child: Child object>": "Child.parent" must be a "Parent" instance.
+# Regression for #12190 -- Should be able to instantiate a FK
+# outside of a model, and interrogate its related field.
+>>> cat = models.ForeignKey(Category)
+>>> cat.rel.get_related_field().name
+'id'
+
"""}
--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to
django-updates+unsubscribe@...
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en-~----------~----~----~----~------~----~------~--~---