【问题标题】:What type of options apply to the SlugField?什么类型的选项适用于 SlugField?
【发布时间】:2019-01-15 23:47:26
【问题描述】:

如果我们看到 Django 的文档,我们会看到这个;

SlugField

class SlugField(max_length=50, **options)

**options : 是一个 kwargs,但 Django 没有告诉我任何其他参数我可以使用它。

如果有人帮助我,我将不胜感激。

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

【问题讨论】:

  • 这些是应用于CharField 的选项,并按程度应用于Field

标签: python django django-models field


【解决方案1】:

简而言之:您可以将这些选项传递给CharField [Django-doc],并传递给Field [Django-doc]

SlugField 是扩展CharField 类的类,我们可以在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)

    #...

所以它默认将max_length 设置为50,将db_index 设置为True,将allow_unicode 设置为False,并将所有位置和命名参数传递给超级构造函数(@ 之一987654337@.

CharField 类也将参数传递给它的超级构造函数,根据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))

这意味着它归结为options one can pass to any Field [Django-doc]。例如:db_columndefaulteditablehelp_text

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-09
    • 2012-01-22
    • 2020-05-05
    • 1970-01-01
    • 2017-06-10
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多