From 48cffd9e459d8aaf9d1afa36f4e07f9624b2c4ad Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Tue, 3 May 2011 11:51:37 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#5931=20--=20Added=20=5F=5Frepr=5F=5F?= =?UTF-8?q?=20to=20db=20fields.=20Thanks,=20Thomas=20G=C3=BCttler,=20emulb?= =?UTF-8?q?reh=20and=20magopian.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@16145 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/__init__.py | 10 ++++++++++ tests/regressiontests/model_fields/tests.py | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index b9604f5006..9037265e8b 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -444,6 +444,16 @@ class Field(object): "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") diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py index 3ec9d7a8b7..b42c0af784 100644 --- a/tests/regressiontests/model_fields/tests.py +++ b/tests/regressiontests/model_fields/tests.py @@ -48,6 +48,15 @@ class BasicFieldTests(test.TestCase): 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), '') + f = models.fields.CharField() + self.assertEqual(repr(f), '') + class DecimalFieldTests(test.TestCase): def test_to_python(self): f = models.DecimalField(max_digits=4, decimal_places=2)