What is the difference between CharField('name',max_length=100)
and CharField(max_length=100)
?
What is the parameter 'name'
used for?
What are the CharField()
constructors?
Can someone give me a link, please?
CharField inherits from class called Field which has constructor
class Field(RegisterLookupMixin):
"""Base class for all field types"""
# <some more code>
def __init__(self, verbose_name=None, name=None, primary_key=False,
max_length=None, unique=False, blank=False, null=False,
db_index=False, rel=None, default=NOT_PROVIDED, editable=True,
serialize=True, unique_for_date=None, unique_for_month=None,
unique_for_year=None, choices=None, help_text='', db_column=None,
db_tablespace=None, auto_created=False, validators=[],
error_messages=None):
Char Field Constructor
class CharField(Field):
description = _("String (up to %(max_length)s)")
def __init__(self, *args, **kwargs):
super(CharField, self).__init__(*args, **kwargs)
self.validators.append(validators.MaxLengthValidator(self.max_length))
The charfield constructor basically passes arguments (keyword and ordinary) to field constructor and adds a maxlength validator.
So the argument 'name' in CharField('name',max_length=100 gets assigned to verbose_name
See this
https://github.com/django/django/blob/master/django/db/models/fields/__init__.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With