Skip to content

Commit

Permalink
Fixed #5931 -- Added __repr__ to db fields. Thanks, Thomas Güttler, e…
Browse files Browse the repository at this point in the history
…mulbreh and magopian.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16145 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jezdez committed May 3, 2011
1 parent 18d2f4a commit 48cffd9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions django/db/models/fields/__init__.py
Expand Up @@ -444,6 +444,16 @@ def value_from_object(self, obj):
"Returns the value of this field in the given model instance."
return getattr(obj, self.attname)

def __repr__(self):
"""
Displays the module, class and name of the field.
"""
path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__)
name = getattr(self, 'name', None)
if name is not None:
return '<%s: %s>' % (path, name)
return '<%s>' % path

class AutoField(Field):
description = _("Integer")

Expand Down
9 changes: 9 additions & 0 deletions tests/regressiontests/model_fields/tests.py
Expand Up @@ -48,6 +48,15 @@ def test_nullbooleanfield_blank(self):
except ValidationError, e:
self.fail("NullBooleanField failed validation with value of None: %s" % e.messages)

def test_field_repr(self):
"""
Regression test for #5931: __repr__ of a field also displays its name
"""
f = Foo._meta.get_field('a')
self.assertEqual(repr(f), '<django.db.models.fields.CharField: a>')
f = models.fields.CharField()
self.assertEqual(repr(f), '<django.db.models.fields.CharField>')

class DecimalFieldTests(test.TestCase):
def test_to_python(self):
f = models.DecimalField(max_digits=4, decimal_places=2)
Expand Down

0 comments on commit 48cffd9

Please sign in to comment.