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
This commit is contained in:
Adrian Holovaty 2005-07-19 17:20:37 +00:00
parent e243d82874
commit df66763406
5 changed files with 59 additions and 49 deletions

View File

@ -1564,13 +1564,14 @@ class Field(object):
# database level. # database level.
empty_strings_allowed = True empty_strings_allowed = True
def __init__(self, name, verbose_name, primary_key=False, def __init__(self, name, verbose_name=None, primary_key=False,
maxlength=None, unique=False, blank=False, null=False, db_index=None, maxlength=None, unique=False, blank=False, null=False, db_index=None,
core=False, rel=None, default=NOT_PROVIDED, editable=True, core=False, rel=None, default=NOT_PROVIDED, editable=True,
prepopulate_from=None, unique_for_date=None, unique_for_month=None, prepopulate_from=None, unique_for_date=None, unique_for_month=None,
unique_for_year=None, validator_list=None, choices=None, radio_admin=None, unique_for_year=None, validator_list=None, choices=None, radio_admin=None,
help_text=''): help_text=''):
self.name, self.verbose_name = name, verbose_name self.name = name
self.verbose_name = verbose_name or name.replace('_', ' ')
self.primary_key = primary_key self.primary_key = primary_key
self.maxlength, self.unique = maxlength, unique self.maxlength, self.unique = maxlength, unique
self.blank, self.null = blank, null self.blank, self.null = blank, null
@ -1767,9 +1768,9 @@ class AutoField(Field):
return Field.get_manipulator_new_data(self, new_data, rel) return Field.get_manipulator_new_data(self, new_data, rel)
class BooleanField(Field): class BooleanField(Field):
def __init__(self, name, verbose_name, **kwargs): def __init__(self, *args, **kwargs):
kwargs['blank'] = True kwargs['blank'] = True
Field.__init__(self, name, verbose_name, **kwargs) Field.__init__(self, *args, **kwargs)
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [formfields.CheckboxField] return [formfields.CheckboxField]
@ -1784,7 +1785,7 @@ class CommaSeparatedIntegerField(CharField):
class DateField(Field): class DateField(Field):
empty_strings_allowed = False empty_strings_allowed = False
def __init__(self, name, verbose_name, auto_now=False, auto_now_add=False, **kwargs): def __init__(self, name, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):
self.auto_now, self.auto_now_add = auto_now, auto_now_add self.auto_now, self.auto_now_add = auto_now, auto_now_add
if auto_now or auto_now_add: if auto_now or auto_now_add:
kwargs['editable'] = False kwargs['editable'] = False
@ -1840,7 +1841,7 @@ class EmailField(Field):
return [formfields.EmailField] return [formfields.EmailField]
class FileField(Field): class FileField(Field):
def __init__(self, name, verbose_name, upload_to='', **kwargs): def __init__(self, name, verbose_name=None, upload_to='', **kwargs):
self.upload_to = upload_to self.upload_to = upload_to
Field.__init__(self, name, verbose_name, **kwargs) Field.__init__(self, name, verbose_name, **kwargs)
@ -1905,7 +1906,7 @@ class FileField(Field):
class FloatField(Field): class FloatField(Field):
empty_strings_allowed = False empty_strings_allowed = False
def __init__(self, name, verbose_name, max_digits, decimal_places, **kwargs): def __init__(self, name, verbose_name=None, max_digits=None, decimal_places=None, **kwargs):
self.max_digits, self.decimal_places = max_digits, decimal_places self.max_digits, self.decimal_places = max_digits, decimal_places
Field.__init__(self, name, verbose_name, **kwargs) Field.__init__(self, name, verbose_name, **kwargs)
@ -1913,7 +1914,7 @@ class FloatField(Field):
return [curry(formfields.FloatField, max_digits=self.max_digits, decimal_places=self.decimal_places)] return [curry(formfields.FloatField, max_digits=self.max_digits, decimal_places=self.decimal_places)]
class ImageField(FileField): class ImageField(FileField):
def __init__(self, name, verbose_name, width_field=None, height_field=None, **kwargs): def __init__(self, name, verbose_name=None, width_field=None, height_field=None, **kwargs):
self.width_field, self.height_field = width_field, height_field self.width_field, self.height_field = width_field, height_field
FileField.__init__(self, name, verbose_name, **kwargs) FileField.__init__(self, name, verbose_name, **kwargs)
@ -1938,17 +1939,17 @@ class IntegerField(Field):
return [formfields.IntegerField] return [formfields.IntegerField]
class IPAddressField(Field): class IPAddressField(Field):
def __init__(self, name, verbose_name, **kwargs): def __init__(self, *args, **kwargs):
kwargs['maxlength'] = 15 kwargs['maxlength'] = 15
Field.__init__(self, name, verbose_name, **kwargs) Field.__init__(self, *args, **kwargs)
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [formfields.IPAddressField] return [formfields.IPAddressField]
class NullBooleanField(Field): class NullBooleanField(Field):
def __init__(self, name, verbose_name, **kwargs): def __init__(self, *args, **kwargs):
kwargs['null'] = True kwargs['null'] = True
Field.__init__(self, name, verbose_name, **kwargs) Field.__init__(self, *args, **kwargs)
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [formfields.NullBooleanField] return [formfields.NullBooleanField]
@ -1966,13 +1967,13 @@ class PositiveSmallIntegerField(IntegerField):
return [formfields.PositiveSmallIntegerField] return [formfields.PositiveSmallIntegerField]
class SlugField(Field): class SlugField(Field):
def __init__(self, name, verbose_name, **kwargs): def __init__(self, *args, **kwargs):
kwargs['maxlength'] = 50 kwargs['maxlength'] = 50
kwargs.setdefault('validator_list', []).append(validators.isAlphaNumeric) kwargs.setdefault('validator_list', []).append(validators.isAlphaNumeric)
# Set db_index=True unless it's been set manually. # Set db_index=True unless it's been set manually.
if not kwargs.has_key('db_index'): if not kwargs.has_key('db_index'):
kwargs['db_index'] = True kwargs['db_index'] = True
Field.__init__(self, name, verbose_name, **kwargs) Field.__init__(self, *args, **kwargs)
def get_manipulator_field_objs(self): def get_manipulator_field_objs(self):
return [formfields.TextField] return [formfields.TextField]
@ -1987,7 +1988,7 @@ class TextField(Field):
class TimeField(Field): class TimeField(Field):
empty_strings_allowed = False empty_strings_allowed = False
def __init__(self, name, verbose_name, auto_now=False, auto_now_add=False, **kwargs): def __init__(self, name, verbose_name=None, auto_now=False, auto_now_add=False, **kwargs):
self.auto_now, self.auto_now_add = auto_now, auto_now_add self.auto_now, self.auto_now_add = auto_now, auto_now_add
if auto_now or auto_now_add: if auto_now or auto_now_add:
kwargs['editable'] = False kwargs['editable'] = False
@ -2014,7 +2015,7 @@ class TimeField(Field):
return [formfields.TimeField] return [formfields.TimeField]
class URLField(Field): class URLField(Field):
def __init__(self, name, verbose_name, verify_exists=True, **kwargs): def __init__(self, name, verbose_name=None, verify_exists=True, **kwargs):
if verify_exists: if verify_exists:
kwargs.setdefault('validator_list', []).append(validators.isExistingURL) kwargs.setdefault('validator_list', []).append(validators.isExistingURL)
Field.__init__(self, name, verbose_name, **kwargs) Field.__init__(self, name, verbose_name, **kwargs)
@ -2027,7 +2028,7 @@ class USStateField(Field):
return [formfields.USStateField] return [formfields.USStateField]
class XMLField(Field): class XMLField(Field):
def __init__(self, name, verbose_name, schema_path, **kwargs): def __init__(self, name, verbose_name=None, schema_path=None, **kwargs):
self.schema_path = schema_path self.schema_path = schema_path
Field.__init__(self, name, verbose_name, **kwargs) Field.__init__(self, name, verbose_name, **kwargs)

View File

@ -12,18 +12,18 @@ Throughout this reference, we'll refer to the following Poll application::
class Poll(meta.Model): class Poll(meta.Model):
fields = ( fields = (
meta.SlugField('slug', 'slug', unique_for_month='pub_date'), meta.SlugField('slug', unique_for_month='pub_date'),
meta.CharField('question', 'question', maxlength=255), meta.CharField('question', maxlength=255),
meta.DateTimeField('pub_date', 'date published'), meta.DateTimeField('pub_date'),
meta.DateTimeField('expire_date', 'expiration date'), meta.DateTimeField('expire_date'),
) )
class Choice(meta.Model): class Choice(meta.Model):
fields = ( fields = (
meta.ForeignKey(Poll, edit_inline=True, edit_inline_type=meta.TABULAR, meta.ForeignKey(Poll, edit_inline=True, edit_inline_type=meta.TABULAR,
num_in_admin=10, min_num_in_admin=5), num_in_admin=10, min_num_in_admin=5),
meta.CharField('choice', 'choice', maxlength=255, core=True), meta.CharField('choice', maxlength=255, core=True),
meta.IntegerField('votes', 'votes', editable=False, default=0), meta.IntegerField('votes', editable=False, default=0),
) )
Basic lookup functions Basic lookup functions
@ -351,6 +351,4 @@ the relation (``poll_id`` in this case).
Deleting objects Deleting objects
================ ================
Just cause we're crazy like that, the delete method is named ``delete()``. The delete method, conveniently, is named ``delete()``.
Yeah, you never know what we're going to do next.

View File

@ -34,9 +34,9 @@ wide array of options, only ``fields`` is required.
A list of field objects. See `Field objects`_. For example:: A list of field objects. See `Field objects`_. For example::
fields = ( fields = (
meta.CharField('customer_name', 'customer name', maxlength=15), meta.CharField('customer_name', maxlength=15),
meta.BooleanField('use_extra_cheese', 'use extra cheese'), meta.BooleanField('use_extra_cheese'),
meta.IntegerField('customer_type', 'customer type', choices=CUSTOMER_TYPE_CHOICES), meta.IntegerField('customer_type', choices=CUSTOMER_TYPE_CHOICES),
... ...
) )
@ -123,10 +123,13 @@ the ``fields`` list is an instance of a ``meta.Field`` subclass and maps to
a database field. a database field.
All field objects -- except for ``ForeignKey`` and ``ManyToManyField`` (see All field objects -- except for ``ForeignKey`` and ``ManyToManyField`` (see
below) -- take two positional arguments and a number of keyword arguments. below) -- require the field's machine-readable name as the first positional
The positional arguments are the field name and the human-readable name. The argument. This must be a valid Python identifier -- no spaces, punctuation,
field name must be a valid Python identifier, but the human-readable name can etc., are allowed.
contain spaces, punctuation, etc.
The second positional argument, a human-readable name, is optional. If the
human-readable name isn't given, Django will use the machine-readable name,
coverting underscores to spaces.
General field options General field options
--------------------- ---------------------
@ -226,7 +229,7 @@ Field Types
use this directly; a primary key field will automatically be added to your use this directly; a primary key field will automatically be added to your
model if you don't specify otherwise. That automatically-added field is:: model if you don't specify otherwise. That automatically-added field is::
meta.AutoField('id', 'ID', primary_key=True) meta.AutoField('id', primary_key=True)
``BooleanField`` ``BooleanField``
A true/false field. A true/false field.

View File

@ -22,7 +22,7 @@ solving two years' worth of database-schema problems. Here's a quick example::
class Reporter(meta.Model): class Reporter(meta.Model):
fields = ( fields = (
meta.CharField('full_name', "reporter's full name", maxlength=70), meta.CharField('full_name', maxlength=70),
) )
def __repr__(self): def __repr__(self):
@ -30,9 +30,9 @@ solving two years' worth of database-schema problems. Here's a quick example::
class Article(meta.Model): class Article(meta.Model):
fields = ( fields = (
meta.DateTimeField('pub_date', 'publication date'), meta.DateTimeField('pub_date'),
meta.CharField('headline', 'headline', maxlength=200), meta.CharField('headline', maxlength=200),
meta.TextField('article', 'article'), meta.TextField('article'),
meta.ForeignKey(Reporter), meta.ForeignKey(Reporter),
) )
@ -133,9 +133,9 @@ classes::
class Article(meta.Model): class Article(meta.Model):
fields = ( fields = (
meta.DateTimeField('pub_date', 'publication date'), meta.DateTimeField('pub_date'),
meta.CharField('headline', 'headline', maxlength=200), meta.CharField('headline', maxlength=200),
meta.TextField('article', 'article'), meta.TextField('article'),
meta.ForeignKey(Reporter), meta.ForeignKey(Reporter),
) )
admin = meta.Admin( admin = meta.Admin(
@ -302,7 +302,11 @@ features:
* An RSS framework that makes creating RSS feeds as easy as writing a * An RSS framework that makes creating RSS feeds as easy as writing a
small Python class. small Python class.
* More sexy automatically-generated admin features -- this overview barely * More sexy automatically-generated admin features -- this overview barely
scratched the surface scratched the surface.
The next obvious steps are for you to download Django, read the documentation The next obvious steps are for you to `download Django`_, read `the tutorial`_
and join the community. Thanks for your interest! and join `the community`_. Thanks for your interest!
.. _download Django: http://www.djangoproject.com/documentation/
.. _the tutorial: http://www.djangoproject.com/documentation/tutorial1/
.. _the community: http://www.djangoproject.com/community/

View File

@ -137,15 +137,15 @@ Edit the ``polls/models/polls.py`` file so that it looks like this::
class Poll(meta.Model): class Poll(meta.Model):
fields = ( fields = (
meta.CharField('question', 'question', maxlength=200), meta.CharField('question', maxlength=200),
meta.DateTimeField('pub_date', 'date published'), meta.DateTimeField('pub_date', 'date published'),
) )
class Choice(meta.Model): class Choice(meta.Model):
fields = ( fields = (
meta.ForeignKey(Poll), meta.ForeignKey(Poll),
meta.CharField('choice', 'choice', maxlength=200), meta.CharField('choice', maxlength=200),
meta.IntegerField('votes', 'votes'), meta.IntegerField('votes'),
) )
The code is straightforward. Each model is represented by a class that 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
machine-friendly format. You'll use this value in your Python code, and your machine-friendly format. You'll use this value in your Python code, and your
database will use it as the column name. database will use it as the column name.
The second argument is the field's human-readable name. That's used in a couple The second, optional, argument is the field's human-readable name. That's used
of introspective parts of Django, and it doubles as documentation. in a couple of introspective parts of Django, and it doubles as documentation.
If this field isn't provided, Django will use the machine-readable name. In
this example, we've only defined a human-readable name for ``Poll.pub_date``.
For all other fields in this model, the field's machine-readable name will
suffice as its human-readable name.
Some ``meta.*Field`` classes have additional required elements. Some ``meta.*Field`` classes have additional required elements.
``meta.CharField``, for example, requires that you give it a ``maxlength``. ``meta.CharField``, for example, requires that you give it a ``maxlength``.