Ticket #10405: 10405.2.patch

File 10405.2.patch, 3.4 KB (added by Chris Wesseling, 13 years ago)

Fixed typo, spotted by GDorn.

  • django/core/exceptions.py

    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.  
    33"""
    44
    55class DjangoRuntimeWarning(RuntimeWarning):
    6    pass
     6    pass
    77
    88class ObjectDoesNotExist(Exception):
    99    "The requested object does not exist"
  • django/db/models/fields/related.py

    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):  
    907907
    908908    def formfield(self, **kwargs):
    909909        db = kwargs.pop('using', None)
     910        if isinstance(self.rel.to, basestring):
     911            raise ValueError("Cannot create form field for %r yet, because "
     912                 "its related model %r has not been loaded yet" % (self.name, self.rel.to))
    910913        defaults = {
    911914            'form_class': forms.ModelChoiceField,
    912915            'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to),
  • tests/regressiontests/forms/tests/models.py

    diff --git a/tests/regressiontests/forms/tests/models.py b/tests/regressiontests/forms/tests/models.py
    index 3f548df..d84d334 100644
    a b  
    11# -*- coding: utf-8 -*-
    22import datetime
    33from django.core.files.uploadedfile import SimpleUploadedFile
     4from django.db import models
     5from django.forms.models import ModelForm, ModelFormMetaclass
    46from django.forms import Form, ModelForm, FileField, ModelChoiceField
    57from django.test import TestCase
    68from regressiontests.forms.models import ChoiceModel, ChoiceOptionModel, ChoiceFieldModel, FileModel, Group, BoundaryModel, Defaults
    79
    8 
    910class ChoiceFieldForm(ModelForm):
    1011    class Meta:
    1112        model = ChoiceFieldModel
    class FormsModelTestCase(TestCase):  
    159160        self.assertEqual(obj.name, u'class default value')
    160161        self.assertEqual(obj.value, 99)
    161162        self.assertEqual(obj.def_date, datetime.date(1999, 3, 2))
     163
     164class 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))
Back to Top