From 43c8d4d35c787a4f2f9afc46a53ea002ff54f76a Mon Sep 17 00:00:00 2001
From: Jonas Obrist <jonas.obrist@divio.ch>
Date: Thu, 9 Jun 2011 23:35:52 +0200
Subject: [PATCH] Fixed 10405 by raising a more useful exception
---
django/core/exceptions.py | 2 +-
django/db/models/fields/related.py | 3 ++
tests/regressiontests/forms/tests/models.py | 34 ++++++++++++++++++++++++++-
3 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/django/core/exceptions.py b/django/core/exceptions.py
index 21be870..201a854 100644
a
|
b
|
Global Django exception and warning classes.
|
3 | 3 | """ |
4 | 4 | |
5 | 5 | class DjangoRuntimeWarning(RuntimeWarning): |
6 | | pass |
| 6 | pass |
7 | 7 | |
8 | 8 | class ObjectDoesNotExist(Exception): |
9 | 9 | "The requested object does not exist" |
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index cedf308..8321f15 100644
a
|
b
|
class ForeignKey(RelatedField, Field):
|
907 | 907 | |
908 | 908 | def formfield(self, **kwargs): |
909 | 909 | db = kwargs.pop('using', None) |
| 910 | if isinstance(self.rel.to, basestring): |
| 911 | raise ValueError("Cannot create form field for %r yet, because " |
| 912 | "it's related model %r has not been loaded yet" % (self.name, self.rel.to)) |
910 | 913 | defaults = { |
911 | 914 | 'form_class': forms.ModelChoiceField, |
912 | 915 | 'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to), |
diff --git a/tests/regressiontests/forms/tests/models.py b/tests/regressiontests/forms/tests/models.py
index 3f548df..d84d334 100644
a
|
b
|
|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | import datetime |
3 | 3 | from django.core.files.uploadedfile import SimpleUploadedFile |
| 4 | from django.db import models |
| 5 | from django.forms.models import ModelForm, ModelFormMetaclass |
4 | 6 | from django.forms import Form, ModelForm, FileField, ModelChoiceField |
5 | 7 | from django.test import TestCase |
6 | 8 | from regressiontests.forms.models import ChoiceModel, ChoiceOptionModel, ChoiceFieldModel, FileModel, Group, BoundaryModel, Defaults |
7 | 9 | |
8 | | |
9 | 10 | class ChoiceFieldForm(ModelForm): |
10 | 11 | class Meta: |
11 | 12 | model = ChoiceFieldModel |
… |
… |
class FormsModelTestCase(TestCase):
|
159 | 160 | self.assertEqual(obj.name, u'class default value') |
160 | 161 | self.assertEqual(obj.value, 99) |
161 | 162 | self.assertEqual(obj.def_date, datetime.date(1999, 3, 2)) |
| 163 | |
| 164 | class RelatedModelFormTests(TestCase): |
| 165 | def test_invalid_loading_order(self): |
| 166 | """ |
| 167 | Test for issue 10405 |
| 168 | """ |
| 169 | class A(models.Model): |
| 170 | ref = models.ForeignKey("B") |
| 171 | |
| 172 | class Meta: |
| 173 | model=A |
| 174 | |
| 175 | self.assertRaises(ValueError, ModelFormMetaclass, 'Form', (ModelForm,), {'Meta': Meta}) |
| 176 | |
| 177 | class B(models.Model): |
| 178 | pass |
| 179 | |
| 180 | def test_valid_loading_order(self): |
| 181 | """ |
| 182 | Test for issue 10405 |
| 183 | """ |
| 184 | class A(models.Model): |
| 185 | ref = models.ForeignKey("B") |
| 186 | |
| 187 | class B(models.Model): |
| 188 | pass |
| 189 | |
| 190 | class Meta: |
| 191 | model=A |
| 192 | |
| 193 | self.assertTrue(issubclass(ModelFormMetaclass('Form', (ModelForm,), {'Meta': Meta}), ModelForm)) |