Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #67 -- Human-readable name is now optional in model fields. If …

…a second positional argument isn't given, Django will use the first argument, converting underscores to spaces. This change is fully backwards-compatible.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@212 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit df66763406cc76e8d0c65b3a857c891c21c25f49 1 parent e243d82
Adrian Holovaty authored July 19, 2005
35  django/core/meta.py
@@ -1564,13 +1564,14 @@ class Field(object):
1564 1564
     # database level.
1565 1565
     empty_strings_allowed = True
1566 1566
 
1567  
-    def __init__(self, name, verbose_name, primary_key=False,
  1567
+    def __init__(self, name, verbose_name=None, primary_key=False,
1568 1568
         maxlength=None, unique=False, blank=False, null=False, db_index=None,
1569 1569
         core=False, rel=None, default=NOT_PROVIDED, editable=True,
1570 1570
         prepopulate_from=None, unique_for_date=None, unique_for_month=None,
1571 1571
         unique_for_year=None, validator_list=None, choices=None, radio_admin=None,
1572 1572
         help_text=''):
1573  
-        self.name, self.verbose_name = name, verbose_name
  1573
+        self.name = name
  1574
+        self.verbose_name = verbose_name or name.replace('_', ' ')
1574 1575
         self.primary_key = primary_key
1575 1576
         self.maxlength, self.unique = maxlength, unique
1576 1577
         self.blank, self.null = blank, null
@@ -1767,9 +1768,9 @@ def get_manipulator_new_data(self, new_data, rel=False):
1767 1768
         return Field.get_manipulator_new_data(self, new_data, rel)
1768 1769
 
1769 1770
 class BooleanField(Field):
1770  
-    def __init__(self, name, verbose_name, **kwargs):
  1771
+    def __init__(self, *args, **kwargs):
1771 1772
         kwargs['blank'] = True
1772  
-        Field.__init__(self, name, verbose_name, **kwargs)
  1773
+        Field.__init__(self, *args, **kwargs)
1773 1774
 
1774 1775
     def get_manipulator_field_objs(self):
1775 1776
         return [formfields.CheckboxField]
@@ -1784,7 +1785,7 @@ def get_manipulator_field_objs(self):
1784 1785
 
1785 1786
 class DateField(Field):
1786 1787
     empty_strings_allowed = False
1787  
-    def __init__(self, name, verbose_name, auto_now=False, auto_now_add=False, **kwargs):
  1788
+    def __init__(self, name, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):
1788 1789
         self.auto_now, self.auto_now_add = auto_now, auto_now_add
1789 1790
         if auto_now or auto_now_add:
1790 1791
             kwargs['editable'] = False
@@ -1840,7 +1841,7 @@ def get_manipulator_field_objs(self):
1840 1841
         return [formfields.EmailField]
1841 1842
 
1842 1843
 class FileField(Field):
1843  
-    def __init__(self, name, verbose_name, upload_to='', **kwargs):
  1844
+    def __init__(self, name, verbose_name=None, upload_to='', **kwargs):
1844 1845
         self.upload_to = upload_to
1845 1846
         Field.__init__(self, name, verbose_name, **kwargs)
1846 1847
 
@@ -1905,7 +1906,7 @@ def get_filename(self, filename):
1905 1906
 
1906 1907
 class FloatField(Field):
1907 1908
     empty_strings_allowed = False
1908  
-    def __init__(self, name, verbose_name, max_digits, decimal_places, **kwargs):
  1909
+    def __init__(self, name, verbose_name=None, max_digits=None, decimal_places=None, **kwargs):
1909 1910
         self.max_digits, self.decimal_places = max_digits, decimal_places
1910 1911
         Field.__init__(self, name, verbose_name, **kwargs)
1911 1912
 
@@ -1913,7 +1914,7 @@ def get_manipulator_field_objs(self):
1913 1914
         return [curry(formfields.FloatField, max_digits=self.max_digits, decimal_places=self.decimal_places)]
1914 1915
 
1915 1916
 class ImageField(FileField):
1916  
-    def __init__(self, name, verbose_name, width_field=None, height_field=None, **kwargs):
  1917
+    def __init__(self, name, verbose_name=None, width_field=None, height_field=None, **kwargs):
1917 1918
         self.width_field, self.height_field = width_field, height_field
1918 1919
         FileField.__init__(self, name, verbose_name, **kwargs)
1919 1920
 
@@ -1938,17 +1939,17 @@ def get_manipulator_field_objs(self):
1938 1939
         return [formfields.IntegerField]
1939 1940
 
1940 1941
 class IPAddressField(Field):
1941  
-    def __init__(self, name, verbose_name, **kwargs):
  1942
+    def __init__(self, *args, **kwargs):
1942 1943
         kwargs['maxlength'] = 15
1943  
-        Field.__init__(self, name, verbose_name, **kwargs)
  1944
+        Field.__init__(self, *args, **kwargs)
1944 1945
 
1945 1946
     def get_manipulator_field_objs(self):
1946 1947
         return [formfields.IPAddressField]
1947 1948
 
1948 1949
 class NullBooleanField(Field):
1949  
-    def __init__(self, name, verbose_name, **kwargs):
  1950
+    def __init__(self, *args, **kwargs):
1950 1951
         kwargs['null'] = True
1951  
-        Field.__init__(self, name, verbose_name, **kwargs)
  1952
+        Field.__init__(self, *args, **kwargs)
1952 1953
 
1953 1954
     def get_manipulator_field_objs(self):
1954 1955
         return [formfields.NullBooleanField]
@@ -1966,13 +1967,13 @@ def get_manipulator_field_objs(self):
1966 1967
         return [formfields.PositiveSmallIntegerField]
1967 1968
 
1968 1969
 class SlugField(Field):
1969  
-    def __init__(self, name, verbose_name, **kwargs):
  1970
+    def __init__(self, *args, **kwargs):
1970 1971
         kwargs['maxlength'] = 50
1971 1972
         kwargs.setdefault('validator_list', []).append(validators.isAlphaNumeric)
1972 1973
         # Set db_index=True unless it's been set manually.
1973 1974
         if not kwargs.has_key('db_index'):
1974 1975
             kwargs['db_index'] = True
1975  
-        Field.__init__(self, name, verbose_name, **kwargs)
  1976
+        Field.__init__(self, *args, **kwargs)
1976 1977
 
1977 1978
     def get_manipulator_field_objs(self):
1978 1979
         return [formfields.TextField]
@@ -1987,7 +1988,7 @@ def get_manipulator_field_objs(self):
1987 1988
 
1988 1989
 class TimeField(Field):
1989 1990
     empty_strings_allowed = False
1990  
-    def __init__(self, name, verbose_name, auto_now=False, auto_now_add=False, **kwargs):
  1991
+    def __init__(self, name, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):
1991 1992
         self.auto_now, self.auto_now_add  = auto_now, auto_now_add
1992 1993
         if auto_now or auto_now_add:
1993 1994
             kwargs['editable'] = False
@@ -2014,7 +2015,7 @@ def get_manipulator_field_objs(self):
2014 2015
         return [formfields.TimeField]
2015 2016
 
2016 2017
 class URLField(Field):
2017  
-    def __init__(self, name, verbose_name, verify_exists=True, **kwargs):
  2018
+    def __init__(self, name, verbose_name=None, verify_exists=True, **kwargs):
2018 2019
         if verify_exists:
2019 2020
             kwargs.setdefault('validator_list', []).append(validators.isExistingURL)
2020 2021
         Field.__init__(self, name, verbose_name, **kwargs)
@@ -2027,7 +2028,7 @@ def get_manipulator_field_objs(self):
2027 2028
         return [formfields.USStateField]
2028 2029
 
2029 2030
 class XMLField(Field):
2030  
-    def __init__(self, name, verbose_name, schema_path, **kwargs):
  2031
+    def __init__(self, name, verbose_name=None, schema_path=None, **kwargs):
2031 2032
         self.schema_path = schema_path
2032 2033
         Field.__init__(self, name, verbose_name, **kwargs)
2033 2034
 
16  docs/db-api.txt
@@ -12,18 +12,18 @@ Throughout this reference, we'll refer to the following Poll application::
12 12
 
13 13
     class Poll(meta.Model):
14 14
         fields = (
15  
-            meta.SlugField('slug', 'slug', unique_for_month='pub_date'),
16  
-            meta.CharField('question', 'question', maxlength=255),
17  
-            meta.DateTimeField('pub_date', 'date published'),
18  
-            meta.DateTimeField('expire_date', 'expiration date'),
  15
+            meta.SlugField('slug', unique_for_month='pub_date'),
  16
+            meta.CharField('question', maxlength=255),
  17
+            meta.DateTimeField('pub_date'),
  18
+            meta.DateTimeField('expire_date'),
19 19
         )
20 20
 
21 21
     class Choice(meta.Model):
22 22
         fields = (
23 23
             meta.ForeignKey(Poll, edit_inline=True, edit_inline_type=meta.TABULAR,
24 24
                 num_in_admin=10, min_num_in_admin=5),
25  
-            meta.CharField('choice', 'choice', maxlength=255, core=True),
26  
-            meta.IntegerField('votes', 'votes', editable=False, default=0),
  25
+            meta.CharField('choice', maxlength=255, core=True),
  26
+            meta.IntegerField('votes', editable=False, default=0),
27 27
         )
28 28
 
29 29
 Basic lookup functions
@@ -351,6 +351,4 @@ the relation (``poll_id`` in this case).
351 351
 Deleting objects
352 352
 ================
353 353
 
354  
-Just cause we're crazy like that, the delete method is named ``delete()``.
355  
-Yeah, you never know what we're going to do next.
356  
-
  354
+The delete method, conveniently, is named ``delete()``.
19  docs/model-api.txt
@@ -34,9 +34,9 @@ wide array of options, only ``fields`` is required.
34 34
     A list of field objects. See `Field objects`_. For example::
35 35
 
36 36
         fields = (
37  
-            meta.CharField('customer_name', 'customer name', maxlength=15),
38  
-            meta.BooleanField('use_extra_cheese', 'use extra cheese'),
39  
-            meta.IntegerField('customer_type', 'customer type', choices=CUSTOMER_TYPE_CHOICES),
  37
+            meta.CharField('customer_name', maxlength=15),
  38
+            meta.BooleanField('use_extra_cheese'),
  39
+            meta.IntegerField('customer_type', choices=CUSTOMER_TYPE_CHOICES),
40 40
             ...
41 41
         )
42 42
 
@@ -123,10 +123,13 @@ the ``fields`` list is an instance of a ``meta.Field`` subclass and maps to
123 123
 a database field.
124 124
 
125 125
 All field objects -- except for ``ForeignKey`` and ``ManyToManyField`` (see
126  
-below) -- take two positional arguments and a number of keyword arguments.
127  
-The positional arguments are the field name and the human-readable name.  The
128  
-field name must be a valid Python identifier, but the human-readable name can
129  
-contain spaces, punctuation, etc.
  126
+below) -- require the field's machine-readable name as the first positional
  127
+argument. This must be a valid Python identifier -- no spaces, punctuation,
  128
+etc., are allowed.
  129
+
  130
+The second positional argument, a human-readable name, is optional. If the
  131
+human-readable name isn't given, Django will use the machine-readable name,
  132
+coverting underscores to spaces.
130 133
 
131 134
 General field options
132 135
 ---------------------
@@ -226,7 +229,7 @@ Field Types
226 229
     use this directly; a primary key field will automatically be added to your
227 230
     model if you don't specify otherwise. That automatically-added field is::
228 231
 
229  
-        meta.AutoField('id', 'ID', primary_key=True)
  232
+        meta.AutoField('id', primary_key=True)
230 233
 
231 234
 ``BooleanField``
232 235
     A true/false field.
24  docs/overview.txt
@@ -22,7 +22,7 @@ solving two years' worth of database-schema problems. Here's a quick example::
22 22
 
23 23
     class Reporter(meta.Model):
24 24
         fields = (
25  
-            meta.CharField('full_name', "reporter's full name", maxlength=70),
  25
+            meta.CharField('full_name', maxlength=70),
26 26
         )
27 27
 
28 28
         def __repr__(self):
@@ -30,9 +30,9 @@ solving two years' worth of database-schema problems. Here's a quick example::
30 30
 
31 31
     class Article(meta.Model):
32 32
         fields = (
33  
-            meta.DateTimeField('pub_date', 'publication date'),
34  
-            meta.CharField('headline', 'headline', maxlength=200),
35  
-            meta.TextField('article', 'article'),
  33
+            meta.DateTimeField('pub_date'),
  34
+            meta.CharField('headline', maxlength=200),
  35
+            meta.TextField('article'),
36 36
             meta.ForeignKey(Reporter),
37 37
         )
38 38
 
@@ -133,9 +133,9 @@ classes::
133 133
 
134 134
     class Article(meta.Model):
135 135
         fields = (
136  
-            meta.DateTimeField('pub_date', 'publication date'),
137  
-            meta.CharField('headline', 'headline', maxlength=200),
138  
-            meta.TextField('article', 'article'),
  136
+            meta.DateTimeField('pub_date'),
  137
+            meta.CharField('headline', maxlength=200),
  138
+            meta.TextField('article'),
139 139
             meta.ForeignKey(Reporter),
140 140
         )
141 141
         admin = meta.Admin(
@@ -302,7 +302,11 @@ features:
302 302
     * An RSS framework that makes creating RSS feeds as easy as writing a
303 303
       small Python class.
304 304
     * More sexy automatically-generated admin features -- this overview barely
305  
-      scratched the surface
  305
+      scratched the surface.
306 306
 
307  
-The next obvious steps are for you to download Django, read the documentation
308  
-and join the community. Thanks for your interest!
  307
+The next obvious steps are for you to `download Django`_, read `the tutorial`_
  308
+and join `the community`_. Thanks for your interest!
  309
+
  310
+.. _download Django: http://www.djangoproject.com/documentation/
  311
+.. _the tutorial: http://www.djangoproject.com/documentation/tutorial1/
  312
+.. _the community: http://www.djangoproject.com/community/
14  docs/tutorial01.txt
@@ -137,15 +137,15 @@ Edit the ``polls/models/polls.py`` file so that it looks like this::
137 137
 
138 138
     class Poll(meta.Model):
139 139
         fields = (
140  
-            meta.CharField('question', 'question', maxlength=200),
  140
+            meta.CharField('question', maxlength=200),
141 141
             meta.DateTimeField('pub_date', 'date published'),
142 142
         )
143 143
 
144 144
     class Choice(meta.Model):
145 145
         fields = (
146 146
             meta.ForeignKey(Poll),
147  
-            meta.CharField('choice', 'choice', maxlength=200),
148  
-            meta.IntegerField('votes', 'votes'),
  147
+            meta.CharField('choice', maxlength=200),
  148
+            meta.IntegerField('votes'),
149 149
         )
150 150
 
151 151
 The code is straightforward. Each model is represented by a class that
@@ -160,8 +160,12 @@ The first argument to each ``Field`` call is the field's name, in
160 160
 machine-friendly format. You'll use this value in your Python code, and your
161 161
 database will use it as the column name.
162 162
 
163  
-The second argument is the field's human-readable name. That's used in a couple
164  
-of introspective parts of Django, and it doubles as documentation.
  163
+The second, optional, argument is the field's human-readable name. That's used
  164
+in a couple of introspective parts of Django, and it doubles as documentation.
  165
+If this field isn't provided, Django will use the machine-readable name. In
  166
+this example, we've only defined a human-readable name for ``Poll.pub_date``.
  167
+For all other fields in this model, the field's machine-readable name will
  168
+suffice as its human-readable name.
165 169
 
166 170
 Some ``meta.*Field`` classes have additional required elements.
167 171
 ``meta.CharField``, for example, requires that you give it a ``maxlength``.

0 notes on commit df66763

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