Ticket #5931: 5931-__repr__-for-db-fields

File 5931-__repr__-for-db-fields, 1.5 KB (added by Mathieu Agopian, 14 years ago)

updated patch that applies to the current trunk

Line 
1diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
2index b9604f5..93a61f3 100644
3--- a/django/db/models/fields/__init__.py
4+++ b/django/db/models/fields/__init__.py
5@@ -444,6 +444,14 @@ class Field(object):
6 "Returns the value of this field in the given model instance."
7 return getattr(obj, self.attname)
8
9+ def __repr__(self):
10+ """
11+ Displays the module, class and name of the field.
12+ """
13+ return '<%s.%s %s>' % (self.__class__.__module__,
14+ self.__class__.__name__,
15+ getattr(self, 'name', None))
16+
17 class AutoField(Field):
18 description = _("Integer")
19
20diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
21index 3ec9d7a..cc13be7 100644
22--- a/tests/regressiontests/model_fields/tests.py
23+++ b/tests/regressiontests/model_fields/tests.py
24@@ -47,6 +47,13 @@ class BasicFieldTests(test.TestCase):
25 nullboolean.full_clean()
26 except ValidationError, e:
27 self.fail("NullBooleanField failed validation with value of None: %s" % e.messages)
28+
29+ def test_field_repr(self):
30+ """
31+ Regression test for #5931: __repr__ of a field also displays its name
32+ """
33+ f = Foo._meta.get_field('a')
34+ self.assertEqual(repr(f), '<django.db.models.fields.CharField a>')
35
36 class DecimalFieldTests(test.TestCase):
37 def test_to_python(self):
Back to Top