Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python **Django中的额外_字段参数_Python_Django - Fatal编程技术网

Python **Django中的额外_字段参数

Python **Django中的额外_字段参数,python,django,Python,Django,我正试图使用以下方法在Django中创建一个自定义用户模型 本文中给出了一个模型管理器: from django.contrib.auth.base_user import BaseUserManager from django.utils.translation import ugettext_lazy as _ class CustomUserManager(BaseUserManager): """ Custom user model ma

我正试图使用以下方法在Django中创建一个自定义用户模型

本文中给出了一个模型管理器:

from django.contrib.auth.base_user import BaseUserManager
from django.utils.translation import ugettext_lazy as _


class CustomUserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self, email, password, **extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError(_('The Email must be set'))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))
        if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
        return self.create_user(email, password, **extra_fields)
我不明白的是,
**extra\u fields
在create\u user和create\u superuser函数中作为参数传递

什么是**额外文件?它的目的是什么,它在这里做什么


提前谢谢

这称为kwarg,它允许您传递任意数量的关键字参数。因此,在您的情况下,您可以将任何其他字段传递给函数,如果它们以相同的名称存在,它将保存在您的模型中


请按照所提到的方法文档查看此SO:


基本上,它与任何函数中的任意关键字参数相同。您可以通过
create\u user
create\u superuser
方法传递任何关键字参数,并且将基于这些参数(如果它们存在于用户模型中)创建用户实例。

**使用额外的\u字段
,以便使用任意数量的关键字参数(kwarg)可以传递给函数create_user和create_superuser。因此,如果传递给create_user和create_superuser的参数位于关联的自定义用户模型中,则将创建一个包含所有这些信息的用户模型


它还用于访问默认用户模型的内置字段,如is_superuser、is_staff和is_active,并为其分配默认值

我可以建议您执行全部操作吗?
The ``extra_fields`` keyword arguments are passed through to the
:class:`~django.contrib.auth.models.User`’s ``__init__`` method to
allow setting arbitrary fields on a :ref:`custom user model