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):
|
223 | 223 | unique = property(unique) |
224 | 224 | |
225 | 225 | def set_attributes_from_name(self, name): |
226 | | self.name = name |
| 226 | if not self.name: |
| 227 | self.name = name |
227 | 228 | 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('_', ' ') |
230 | 231 | |
231 | 232 | def contribute_to_class(self, cls, name): |
232 | 233 | self.set_attributes_from_name(name) |
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):
|
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 | |
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
|
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: |
… |
… |
class BasicFieldTests(test.TestCase):
|
57 | 57 | f = models.fields.CharField() |
58 | 58 | self.assertEqual(repr(f), '<django.db.models.fields.CharField>') |
59 | 59 | |
| 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 | |
60 | 69 | class DecimalFieldTests(test.TestCase): |
61 | 70 | def test_to_python(self): |
62 | 71 | f = models.DecimalField(max_digits=4, decimal_places=2) |