I need my django users to belong to an Office.
我需要我的django用户属于Office。
I did the following to extend the default user model:
我做了以下扩展默认用户模型:
class UserProfile(models.Model):
user = models.OneToOneField(User)
office = models.ForeignKey('Office')
My post_save signal for the user model calls this function:
我的用户模型的post_save信号调用此函数:
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile = UserProfile.objects.create(
user = instance,
office = instance.office
)
I basically want to attach office to "instance". Which means when I create the user I do the following:
我基本上想把办公室附加到“实例”。这意味着当我创建用户时,我会执行以下操作:
user = User(
office = office,
username = username,
email = email,
password = password
)
But I get the error:
但我得到错误:
'office' is an invalid keyword argument for this function
'office'是此函数的无效关键字参数
Which is normal because office is not a field for the User model. But is there a way to tell django to ignore this field, that this field is only here to "carry" information temporarily?
这是正常的,因为office不是User模型的字段。但有没有办法告诉django忽略这个字段,这个字段只是暂时“携带”信息?
1 个解决方案
#1
1
Just set the office
as a simple attribute:
只需将办公室设置为一个简单的属性:
user = User(username=username, email=email)
user.set_password(password)
user.office = office
user.save()
#1
1
Just set the office
as a simple attribute:
只需将办公室设置为一个简单的属性:
user = User(username=username, email=email)
user.set_password(password)
user.office = office
user.save()