在Django用户模型中需要first_name和last_name [英] Requiring first_name and last_name in Django User model
问题描述
在 django.contrib.auth.models.User
中, first_name
和 last_name
字段有 blank = True
。我如何使自己的模型中的 blank = False,null = False
In django.contrib.auth.models.User
, both first_name
and last_name
fields have blank=True
. How can I make them blank=False, null=False
in my own model?
这是我的实现,其基本遵循扩展的说明现有的用户模型:
Here is my implementation, which basically follows the instructions of Extending the existing User model:
models.py
models.py
class Fellow(models.Model):
user = models.OneToOneField(
User,
on_delete=models.CASCADE
)
first_name = models.CharField(
_("first_name"),
max_length=30,
)
last_name = models.CharField(
_("last_name"),
max_length=30,
)
# other fields omitted
from . import signals
signals.py
signals.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Fellow
@receiver(post_save, sender=User)
def create_fellow_on_user_create(sender, instance, created, **kwargs):
if created:
Fellow.objects.create(user=instance)
但是,在 python manage.py shell
中测试时出现错误:
However, I got an error when testing in python manage.py shell
:
>>> f = Fellow.objects.create(username='username', password='passwd')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/query.py", line 392, in create
obj = self.model(**kwargs)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/db/models/base.py", line 571, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'username' is an invalid keyword argument for this function
推荐答案
你误解了教程。您所指的员工示例说明如何添加到用户
中,这是通过一个 OneToOneField
原始用户
模型完好无损,但对于所有实际用途,就好像用户
有一个新的字段称为部门
。这是通过在数据库中创建两个单独的表来实现的。
You have misunderstood the tutorial. The employee example you are referring to explains how to add to the User
this is done by way of a OneToOneField
that leaves the original User
model intact but for all practical purposes it's as if the User
had a new field called department
. This is implemented by creating two separate tables in the database.
在 Employee
和 Fellow $ c $中有一个警告c>您不能在没有先创建
用户
实例的情况下直接创建新实例。这是因为这些模型都不直接拥有用户名字段。所以这样做两步
There is one caveat, in the Employee
and the Fellow
you can't directly create new instances in the way you have tried without first creating a User
instance. That's because neither of those models directly own a username field. So do it in two steps
user = User(username='Sun')
user.set_password('123')
user.save()
f = Fellow(user=user)
但是这种方法还不太理想。您有两个模型中的first_name和last_name字段。总会导致混乱。正确的方法是将AbstractBaseUser
But this approach is still less than desirable. you have a first_name and last_name field in both models. It will always lead to confusion. The proper method is to subclass AbstractBaseUser
class Fellow(AbstractBaseUser):
first_name = models.CharField(max_length=254)
AbstractBaseUser
是一个裸体用户(请参阅代码: https: //github.com/django/django/blob/master/django/contrib/auth/base_user.py#L47 ),它没有first_name和last_name,因此您可以使用所需的行为添加它们。
AbstractBaseUser
is a bare bones User (see the code: https://github.com/django/django/blob/master/django/contrib/auth/base_user.py#L47) it does not have a first_name and last_name so you can add them with the behaviour that you want.
这篇关于在Django用户模型中需要first_name和last_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!