Author: jezdez
Date: 2011-08-12 07:15:08 -0700 (Fri, 12 Aug 2011)
New Revision: 16604

Modified:
   django/trunk/django/core/exceptions.py
   django/trunk/django/db/models/fields/related.py
   django/trunk/tests/regressiontests/forms/tests/models.py
Log:
Fixed #10405 -- Raise a more useful error if the formfield of a related model 
field can't be created yet because the related model isn't loaded yet. Thanks 
ojii and charstring.

Modified: django/trunk/django/core/exceptions.py
===================================================================
--- django/trunk/django/core/exceptions.py      2011-08-12 14:14:58 UTC (rev 
16603)
+++ django/trunk/django/core/exceptions.py      2011-08-12 14:15:08 UTC (rev 
16604)
@@ -3,7 +3,7 @@
 """
 
 class DjangoRuntimeWarning(RuntimeWarning):
-   pass
+    pass
 
 class ObjectDoesNotExist(Exception):
     "The requested object does not exist"

Modified: django/trunk/django/db/models/fields/related.py
===================================================================
--- django/trunk/django/db/models/fields/related.py     2011-08-12 14:14:58 UTC 
(rev 16603)
+++ django/trunk/django/db/models/fields/related.py     2011-08-12 14:15:08 UTC 
(rev 16604)
@@ -905,6 +905,10 @@
 
     def formfield(self, **kwargs):
         db = kwargs.pop('using', None)
+        if isinstance(self.rel.to, basestring):
+            raise ValueError("Cannot create form field for %r yet, because "
+                             "its related model %r has not been loaded yet" %
+                             (self.name, self.rel.to))
         defaults = {
             'form_class': forms.ModelChoiceField,
             'queryset': 
self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to),

Modified: django/trunk/tests/regressiontests/forms/tests/models.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests/models.py    2011-08-12 
14:14:58 UTC (rev 16603)
+++ django/trunk/tests/regressiontests/forms/tests/models.py    2011-08-12 
14:15:08 UTC (rev 16604)
@@ -1,7 +1,9 @@
 # -*- coding: utf-8 -*-
 import datetime
 from django.core.files.uploadedfile import SimpleUploadedFile
+from django.db import models
 from django.forms import Form, ModelForm, FileField, ModelChoiceField
+from django.forms.models import ModelFormMetaclass
 from django.test import TestCase
 from regressiontests.forms.models import (ChoiceOptionModel, ChoiceFieldModel,
     FileModel, Group, BoundaryModel, Defaults)
@@ -160,3 +162,34 @@
         self.assertEqual(obj.name, u'class default value')
         self.assertEqual(obj.value, 99)
         self.assertEqual(obj.def_date, datetime.date(1999, 3, 2))
+
+class RelatedModelFormTests(TestCase):
+    def test_invalid_loading_order(self):
+        """
+        Test for issue 10405
+        """
+        class A(models.Model):
+            ref = models.ForeignKey("B")
+
+        class Meta:
+            model=A
+
+        self.assertRaises(ValueError, ModelFormMetaclass, 'Form', 
(ModelForm,), {'Meta': Meta})
+
+        class B(models.Model):
+            pass
+
+    def test_valid_loading_order(self):
+        """
+        Test for issue 10405
+        """
+        class A(models.Model):
+            ref = models.ForeignKey("B")
+
+        class B(models.Model):
+            pass
+
+        class Meta:
+            model=A
+
+        self.assertTrue(issubclass(ModelFormMetaclass('Form', (ModelForm,), 
{'Meta': Meta}), ModelForm))

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

Reply via email to