Author: jezdez Date: 2011-05-03 04:51:37 -0700 (Tue, 03 May 2011) New Revision: 16145
Modified: django/trunk/django/db/models/fields/__init__.py django/trunk/tests/regressiontests/model_fields/tests.py Log: Fixed #5931 -- Added __repr__ to db fields. Thanks, Thomas G?\195?\188ttler, emulbreh and magopian. Modified: django/trunk/django/db/models/fields/__init__.py =================================================================== --- django/trunk/django/db/models/fields/__init__.py 2011-05-03 10:44:23 UTC (rev 16144) +++ django/trunk/django/db/models/fields/__init__.py 2011-05-03 11:51:37 UTC (rev 16145) @@ -444,6 +444,16 @@ "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") Modified: django/trunk/tests/regressiontests/model_fields/tests.py =================================================================== --- django/trunk/tests/regressiontests/model_fields/tests.py 2011-05-03 10:44:23 UTC (rev 16144) +++ django/trunk/tests/regressiontests/model_fields/tests.py 2011-05-03 11:51:37 UTC (rev 16145) @@ -48,6 +48,15 @@ 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) -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.