Fixed #27599 -- Fixed Field.__str__() crash for fields not attached to models.

This commit is contained in:
Morgan Aubert 2016-12-14 14:04:26 -05:00 committed by Tim Graham
parent 3be2268992
commit ef889d5b10
2 changed files with 9 additions and 3 deletions

View File

@ -186,7 +186,12 @@ class Field(RegisterLookupMixin):
self.error_messages = messages self.error_messages = messages
def __str__(self): def __str__(self):
""" Return "app_label.model_label.field_name". """ """
Return "app_label.model_label.field_name" for fields attached to
models.
"""
if not hasattr(self, 'model'):
return super(Field, self).__str__()
model = self.model model = self.model
app = model._meta.app_label app = model._meta.app_label
return '%s.%s.%s' % (app, model._meta.object_name, self.name) return '%s.%s.%s' % (app, model._meta.object_name, self.name)

View File

@ -1,7 +1,6 @@
from django import forms from django import forms
from django.db import models from django.db import models
from django.test import SimpleTestCase, TestCase from django.test import SimpleTestCase, TestCase
from django.utils.encoding import force_str
from .models import ( from .models import (
Foo, RenamedField, VerboseNameField, Whiz, WhizIter, WhizIterEmpty, Foo, RenamedField, VerboseNameField, Whiz, WhizIter, WhizIterEmpty,
@ -56,8 +55,10 @@ class BasicFieldTests(TestCase):
self.assertIsInstance(field.formfield(choices_form_class=klass), klass) self.assertIsInstance(field.formfield(choices_form_class=klass), klass)
def test_field_str(self): def test_field_str(self):
f = models.Field()
self.assertEqual(str(f), '<django.db.models.fields.Field>')
f = Foo._meta.get_field('a') f = Foo._meta.get_field('a')
self.assertEqual(force_str(f), 'model_fields.Foo.a') self.assertEqual(str(f), 'model_fields.Foo.a')
def test_field_ordering(self): def test_field_ordering(self):
"""Fields are ordered based on their creation.""" """Fields are ordered based on their creation."""