[Django]-Parameters for class CharField()

2👍

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

2👍

It’s a verbose field name.

According to the documentation:

Each field type, except for ForeignKey, ManyToManyField and
OneToOneField, takes an optional first positional argument – a verbose
name
. If the verbose name isn’t given, Django will automatically
create it using the field’s attribute name, converting underscores to
spaces.

Here’s a documentation for CharField: https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.CharField

Leave a comment