Ticket #18212: generic_ip_verbose_name.patch

File generic_ip_verbose_name.patch, 4.8 KB (added by toofishes, 3 years ago)
  • tests/modeltests/validation/models.py

     
    8787    generic_ip = models.GenericIPAddressField(blank=True, null=True, unique=True)
    8888    v4_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv4")
    8989    v6_ip = models.GenericIPAddressField(blank=True, null=True, protocol="ipv6")
     90    ip_verbose_name = models.GenericIPAddressField("IP Address Verbose",
     91            blank=True, null=True)
    9092
    9193class GenericIPAddrUnpackUniqueTest(models.Model):
    9294    generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True)
  • tests/regressiontests/model_fields/tests.py

     
    1111from django.utils import unittest
    1212
    1313from .models import (Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post,
    14     NullBooleanModel, BooleanModel, Document, RenamedField)
     14    NullBooleanModel, BooleanModel, Document, RenamedField, VerboseNameField)
    1515
    1616# If PIL available, do these tests.
    1717if Image:
     
    6565        self.assertTrue(hasattr(instance, 'get_fieldname_display'))
    6666        self.assertFalse(hasattr(instance, 'get_modelname_display'))
    6767
     68    def test_field_verbose_name(self):
     69        m = VerboseNameField
     70        for i in range(1, 23):
     71            self.assertEqual(m._meta.get_field('field%d' % i).verbose_name,
     72                    'verbose field%d' % i)
     73
     74        self.assertEqual(m._meta.get_field('id').verbose_name, 'verbose pk')
     75
    6876class DecimalFieldTests(test.TestCase):
    6977    def test_to_python(self):
    7078        f = models.DecimalField(max_digits=4, decimal_places=2)
  • tests/regressiontests/model_fields/models.py

     
    6969class RenamedField(models.Model):
    7070    modelname = models.IntegerField(name="fieldname", choices=((1,'One'),))
    7171
     72class VerboseNameField(models.Model):
     73    id = models.AutoField("verbose pk", primary_key=True)
     74    field1 = models.BigIntegerField("verbose field1")
     75    field2 = models.BooleanField("verbose field2")
     76    field3 = models.CharField("verbose field3", max_length=10)
     77    field4 = models.CommaSeparatedIntegerField("verbose field4", max_length=99)
     78    field5 = models.DateField("verbose field5")
     79    field6 = models.DateTimeField("verbose field6")
     80    field7 = models.DecimalField("verbose field7", max_digits=6, decimal_places=1)
     81    field8 = models.EmailField("verbose field8")
     82    field9 = models.FileField("verbose field9", upload_to="unused")
     83    field10 = models.FilePathField("verbose field10")
     84    field11 = models.FloatField("verbose field11")
     85    # Don't want to depend on PIL in this test
     86    #field_image = models.ImageField("verbose field")
     87    field12 = models.IntegerField("verbose field12")
     88    field13 = models.IPAddressField("verbose field13")
     89    field14 = models.GenericIPAddressField("verbose field14", protocol="ipv4")
     90    field15 = models.NullBooleanField("verbose field15")
     91    field16 = models.PositiveIntegerField("verbose field16")
     92    field17 = models.PositiveSmallIntegerField("verbose field17")
     93    field18 = models.SlugField("verbose field18")
     94    field19 = models.SmallIntegerField("verbose field19")
     95    field20 = models.TextField("verbose field20")
     96    field21 = models.TimeField("verbose field21")
     97    field22 = models.URLField("verbose field22")
     98
    7299# This model isn't used in any test, just here to ensure it validates successfully.
    73100# See ticket #16570.
    74101class DecimalLessThanOne(models.Model):
  • django/db/models/fields/__init__.py

     
    10351035    description = _("IP address")
    10361036    default_error_messages = {}
    10371037
    1038     def __init__(self, protocol='both', unpack_ipv4=False, *args, **kwargs):
     1038    def __init__(self, verbose_name=None, name=None, protocol='both',
     1039                 unpack_ipv4=False, *args, **kwargs):
    10391040        self.unpack_ipv4 = unpack_ipv4
    10401041        self.default_validators, invalid_error_message = \
    10411042            validators.ip_address_validators(protocol, unpack_ipv4)
    10421043        self.default_error_messages['invalid'] = invalid_error_message
    10431044        kwargs['max_length'] = 39
    1044         Field.__init__(self, *args, **kwargs)
     1045        Field.__init__(self, verbose_name, name, *args, **kwargs)
    10451046
    10461047    def get_internal_type(self):
    10471048        return "GenericIPAddressField"
Back to Top