Andres Manuel Diaz
Andres Manuel Diaz

Reputation: 159

What type of options apply to the SlugField?

If we see the documentation of Django, we see this;

SlugField

class SlugField(max_length=50, **options)

**options : is a kwargs but Django doesn't show me anything about of what other parameter I can use it.

I appreciate if somebody help me.

https://docs.djangoproject.com/en/2.1/ref/models/fields/#slugfield

Upvotes: 1

Views: 44

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477607

In short: these are options you can pass to a CharField [Django-doc], and by extent a Field [Django-doc].

A SlugField is a class that extends the CharField class, we can see this in the source code [GitHub]:

class SlugField(CharField):
    default_validators = [validators.validate_slug]
    description = _("Slug (up to %(max_length)s)")

    def __init__(self, *args, max_length=50, db_index=True, allow_unicode=False, **kwargs):
        self.allow_unicode = allow_unicode
        if self.allow_unicode:
            self.default_validators = [validators.validate_unicode_slug]
        super().__init__(*args, max_length=max_length, db_index=db_index, **kwargs)

    #...

So it sets max_length by default to 50, db_index to True and allow_unicode to False, and passes all the positional and named arguments to the super constructor (the one of the CharField.

The CharField class passes the arguments to its super constructor as well, according to the source code [GitHub]:

class CharField(Field):
    description = _("String (up to %(max_length)s)")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.validators.append(validators.MaxLengthValidator(self.max_length))

So that means that it comes down to the options one can pass to any Field [Django-doc]. For example: db_column, default, editable, help_text, etc.

Upvotes: 1

Related Questions