Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 1abafe696e44522e8d71c11a53501aeb70a75d38 1 parent c0a4c04
Jannis Leidel authored August 13, 2011
9  django/db/models/fields/__init__.py
@@ -220,15 +220,16 @@ def db_type(self, connection):
220 220
         except KeyError:
221 221
             return None
222 222
 
  223
+    @property
223 224
     def unique(self):
224 225
         return self._unique or self.primary_key
225  
-    unique = property(unique)
226 226
 
227 227
     def set_attributes_from_name(self, name):
228  
-        self.name = name
  228
+        if not self.name:
  229
+            self.name = name
229 230
         self.attname, self.column = self.get_attname_column()
230  
-        if self.verbose_name is None and name:
231  
-            self.verbose_name = name.replace('_', ' ')
  231
+        if self.verbose_name is None and self.name:
  232
+            self.verbose_name = self.name.replace('_', ' ')
232 233
 
233 234
     def contribute_to_class(self, cls, name):
234 235
         self.set_attributes_from_name(name)
3  tests/regressiontests/model_fields/models.py
@@ -66,6 +66,9 @@ class BooleanModel(models.Model):
66 66
     bfield = models.BooleanField()
67 67
     string = models.CharField(max_length=10, default='abc')
68 68
 
  69
+class RenamedField(models.Model):
  70
+    modelname = models.IntegerField(name="fieldname", choices=((1,'One'),))
  71
+
69 72
 ###############################################################################
70 73
 # FileField
71 74
 
11  tests/regressiontests/model_fields/tests.py
@@ -8,7 +8,7 @@
8 8
 from django.db.models.fields.files import FieldFile
9 9
 from django.utils import unittest
10 10
 
11  
-from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document
  11
+from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document, RenamedField
12 12
 
13 13
 # If PIL available, do these tests.
14 14
 if Image:
@@ -53,6 +53,15 @@ def test_field_repr(self):
53 53
         f = models.fields.CharField()
54 54
         self.assertEqual(repr(f), '<django.db.models.fields.CharField>')
55 55
 
  56
+    def test_field_name(self):
  57
+        """
  58
+        Regression test for #14695: explicitly defined field name overwritten
  59
+        by model's attribute name.
  60
+        """
  61
+        instance = RenamedField()
  62
+        self.assertTrue(hasattr(instance, 'get_fieldname_display'))
  63
+        self.assertFalse(hasattr(instance, 'get_modelname_display'))
  64
+
56 65
 class DecimalFieldTests(test.TestCase):
57 66
     def test_to_python(self):
58 67
         f = models.DecimalField(max_digits=4, decimal_places=2)

0 notes on commit 1abafe6

Please sign in to comment.
Something went wrong with that request. Please try again.