Ticket #14695: ticket14695.patch

File ticket14695.patch, 2.6 KB (added by Will Hardy, 13 years ago)

Adding tests

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

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 9037265..35ec521 100644
    a b class Field(object):  
    223223    unique = property(unique)
    224224
    225225    def set_attributes_from_name(self, name):
    226         self.name = name
     226        if not self.name:
     227            self.name = name
    227228        self.attname, self.column = self.get_attname_column()
    228         if self.verbose_name is None and name:
    229             self.verbose_name = name.replace('_', ' ')
     229        if self.verbose_name is None and self.name:
     230            self.verbose_name = self.name.replace('_', ' ')
    230231
    231232    def contribute_to_class(self, cls, name):
    232233        self.set_attributes_from_name(name)
  • tests/regressiontests/model_fields/models.py

    diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py
    index 1dc1649..8adf7b6 100644
    a b class BooleanModel(models.Model):  
    6666    bfield = models.BooleanField()
    6767    string = models.CharField(max_length=10, default='abc')
    6868
     69class RenamedField(models.Model):
     70    modelname = models.IntegerField(name="fieldname", choices=((1,'One'),))
     71
    6972###############################################################################
    7073# FileField
    7174
  • tests/regressiontests/model_fields/tests.py

    diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
    index b42c0af..aa0c375 100644
    a b from django.db import models  
    88from django.db.models.fields.files import FieldFile
    99from django.utils import unittest
    1010
    11 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document
     11from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document, RenamedField
    1212
    1313# If PIL available, do these tests.
    1414if Image:
    class BasicFieldTests(test.TestCase):  
    5757        f = models.fields.CharField()
    5858        self.assertEqual(repr(f), '<django.db.models.fields.CharField>')
    5959
     60    def test_field_name(self):
     61        """
     62        Regression test for #14695: explicitly defined field name overwritten
     63        by model's attribute name.
     64        """
     65        instance = RenamedField()
     66        self.assertTrue(hasattr(instance, 'get_fieldname_display'))
     67        self.assertFalse(hasattr(instance, 'get_modelname_display'))
     68
    6069class DecimalFieldTests(test.TestCase):
    6170    def test_to_python(self):
    6271        f = models.DecimalField(max_digits=4, decimal_places=2)
Back to Top