[英]Creating multiple types users in django rest framework, got an unexpected keyword argument 'last_login'
I am trying to learn Django, and I am creating various cases just to exercise and hopefully learn something new.我正在尝试学习 Django,并且我正在创建各种案例只是为了锻炼并希望学到新的东西。 Suppose that I am trying to create different users and assign different permissions latter, but first lets try to create them first.
假设我试图创建不同的用户并稍后分配不同的权限,但首先让我们先尝试创建它们。 I was researching last night and it is a common problem and many differnt implementations.
我昨晚在研究,这是一个常见问题,并且有许多不同的实现。 Most common that I have seen is with student, teacher, admin.
我见过的最常见的是学生、老师、管理员。 My case is similar, but instead of teacher it is Staff as staff member.
我的情况类似,但不是老师,而是员工作为员工。 Staff inherits from User, which itself inherits from AbstractBaseUser.
Staff 继承自 User,而 User 本身继承自 AbstractBaseUser。 Below is my code for each class, model and serializer.
下面是我对每个 class、model 和序列化器的代码。 User mode:
用户模式:
class User(AbstractBaseUser):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.EmailField(db_index=True, unique=True)
username = models.CharField(max_length=40, unique=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name','username']
objects = UserManager()
## And user manager:
class UserManager(BaseUserManager):
def get_by_natural_key(self, email):
return self.get(email=email)
Model: Model:
class Staff(User):
qualification = models.CharField(db_index=True, max_length=255)
department = models.CharField(db_index=True, max_length=10)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'department','username']
objects = StaffManager()
def __str__(self):
return self.first_name
Staff Model Manager:员工 Model 经理:
class StaffManager(BaseUserManager):
def create_staff(self, first_name, last_name, email, qualification, department, password=None):
if email is None:
raise TypeError('Users must have an email address.')
staff = Staff(first_name=first_name, last_name=last_name,
email=self.normalize_email(email),
qualification=qualification, department=department)
staff.set_password(password)
staff.save()
return staff
And the Staff Registartion Serializer:和员工注册序列化器:
class StaffRegistrationSerializer(serializers.ModelSerializer):
password = serializers.CharField(
max_length=128,
min_length=8,
write_only=True
)
token = serializers.CharField(max_length=255, read_only=True)
class Meta:
model = Staff
fields = '__all__'
def create(self, validated_data):
return Staff.objects.create_staff(**validated_data)
The problem is that from the API, a field of Last login
is created that it is not declared in models.问题是从 API 中,创建了一个
Last login
字段,它没有在模型中声明。 And when I submit my form, I get this error:当我提交表单时,我收到此错误:
TypeError: create_staff() got an unexpected keyword argument 'last_login'
[17/Dec/2020 15:44:43] "POST /api/auth/register-staff HTTP/1.1" 500 104579
I was looking at this which has the similar problem, but he has a different implementation and at this point, I do not fully understand so I could modify my code to act in the same way.我在看这个有类似问题的,但他有不同的实现,在这一点上,我不完全理解,所以我可以修改我的代码以同样的方式运行。
PS, I am folloing this tutorial to create multiple types of users. PS,我正在按照本教程创建多种类型的用户。
The problem is that **validated_data
has last_login
with a value.问题是
**validated_data
的last_login
有一个值。 So when you're passing in the kwargs
of validated_data
, it gets expanded into the function arguments for create_staff()
but there's no slot for last_login
.因此,当您传入
validated_data
的kwargs
时,它会扩展到 function arguments 用于create_staff()
但没有用于last_login
的插槽。
The fix is to add , last_login=None
to the definition of create_staff
, and pass it along to the new Staff
object.解决方法是将
, last_login=None
添加到create_staff
的定义中,并将其传递给新的Staff
object。 Eg:例如:
def create_staff(self, first_name, last_name, email, qualification, department, password=None, last_login=None):
# code cut for brevity
staff = Staff(first_name=first_name, last_name=last_name,
email=self.normalize_email(email),
qualification=qualification, department=department,
last_login=last_login)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.