From 1abafe696e44522e8d71c11a53501aeb70a75d38 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sat, 13 Aug 2011 11:53:42 +0000 Subject: [PATCH] Fixed #14695 -- Stopped model fields from incorrectly overwriting the 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 --- django/db/models/fields/__init__.py | 9 +++++---- tests/regressiontests/model_fields/models.py | 3 +++ tests/regressiontests/model_fields/tests.py | 11 ++++++++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index b786b7bf11..b9cc64ce3a 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -220,15 +220,16 @@ class Field(object): 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) diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py index 1dc1649f13..8adf7b655b 100644 --- a/tests/regressiontests/model_fields/models.py +++ b/tests/regressiontests/model_fields/models.py @@ -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 diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py index 4944db78e8..faa74cc05b 100644 --- a/tests/regressiontests/model_fields/tests.py +++ b/tests/regressiontests/model_fields/tests.py @@ -8,7 +8,7 @@ from django.db import models 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: @@ -53,6 +53,15 @@ class BasicFieldTests(test.TestCase): f = models.fields.CharField() self.assertEqual(repr(f), '') + 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)