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

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

updated patch taking claudep's and julien's comments into account

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index b9604f5..bcc1f4f 100644
    a b class Field(object):  
    444444        "Returns the value of this field in the given model instance."
    445445        return getattr(obj, self.attname)
    446446
     447    def __repr__(self):
     448        """
     449        Displays the module, class and name of the field.
     450        """
     451        name = getattr(self, 'name', None)
     452        if name is not None:
     453            return '<%s.%s %s>' % (self.__class__.__module__,
     454                                   self.__class__.__name__,
     455                                   name)
     456        return '<%s.%s>' % (self.__class__.__module__, self.__class__.__name__)
     457
    447458class AutoField(Field):
    448459    description = _("Integer")
    449460
  • tests/regressiontests/model_fields/tests.py

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    index 3ec9d7a..f2a041a 100644
    a b class BasicFieldTests(test.TestCase):  
    4747            nullboolean.full_clean()
    4848        except ValidationError, e:
    4949            self.fail("NullBooleanField failed validation with value of None: %s" % e.messages)
     50   
     51    def test_field_repr(self):
     52        """
     53        Regression test for #5931: __repr__ of a field also displays its name
     54        """
     55        f = Foo._meta.get_field('a')
     56        self.assertEqual(repr(f), '<django.db.models.fields.CharField a>')
     57        f = models.fields.CharField()
     58        self.assertEqual(repr(f), '<django.db.models.fields.CharField>')
    5059
    5160class DecimalFieldTests(test.TestCase):
    5261    def test_to_python(self):
Back to Top