Skip to content

Commit

Permalink
Fixed #14695 -- Stopped model fields from incorrectly overwriting the…
Browse files Browse the repository at this point in the history
… field name during model initialization if it was already passed as a keyword argument. Thanks, erikrose and willhardy.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16614 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jezdez committed Aug 13, 2011
1 parent c0a4c04 commit 1abafe6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
9 changes: 5 additions & 4 deletions django/db/models/fields/__init__.py
Expand Up @@ -220,15 +220,16 @@ def db_type(self, connection):
except KeyError:
return None

@property
def unique(self):
return self._unique or self.primary_key
unique = property(unique)

def set_attributes_from_name(self, name):
self.name = name
if not self.name:
self.name = name
self.attname, self.column = self.get_attname_column()
if self.verbose_name is None and name:
self.verbose_name = name.replace('_', ' ')
if self.verbose_name is None and self.name:
self.verbose_name = self.name.replace('_', ' ')

def contribute_to_class(self, cls, name):
self.set_attributes_from_name(name)
Expand Down
3 changes: 3 additions & 0 deletions tests/regressiontests/model_fields/models.py
Expand Up @@ -66,6 +66,9 @@ class BooleanModel(models.Model):
bfield = models.BooleanField()
string = models.CharField(max_length=10, default='abc')

class RenamedField(models.Model):
modelname = models.IntegerField(name="fieldname", choices=((1,'One'),))

###############################################################################
# FileField

Expand Down
11 changes: 10 additions & 1 deletion tests/regressiontests/model_fields/tests.py
Expand Up @@ -8,7 +8,7 @@
from django.db.models.fields.files import FieldFile
from django.utils import unittest

from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document
from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document, RenamedField

# If PIL available, do these tests.
if Image:
Expand Down Expand Up @@ -53,6 +53,15 @@ def test_field_repr(self):
f = models.fields.CharField()
self.assertEqual(repr(f), '<django.db.models.fields.CharField>')

def test_field_name(self):
"""
Regression test for #14695: explicitly defined field name overwritten
by model's attribute name.
"""
instance = RenamedField()
self.assertTrue(hasattr(instance, 'get_fieldname_display'))
self.assertFalse(hasattr(instance, 'get_modelname_display'))

class DecimalFieldTests(test.TestCase):
def test_to_python(self):
f = models.DecimalField(max_digits=4, decimal_places=2)
Expand Down

0 comments on commit 1abafe6

Please sign in to comment.