__init__() 得到了一个意外的关键字参数“用户" [英] __init__() got an unexpected keyword argument 'user'
问题描述
我正在使用 Django 创建用户和创建用户时的对象.但是有一个错误
i am using Django to create a user and an object when the user is created. But there is an error
__init__() 得到一个意外的关键字参数 'user'
调用 view.py 中的 register()
函数时.功能是:
when calling the register()
function in view.py.
The function is:
def register(request):
'''signup view'''
if request.method=="POST":
form=RegisterForm(request.POST)
if form.is_valid():
username=form.cleaned_data["username"]
email=form.cleaned_data["email"]
password=form.cleaned_data["password"]
user=User.objects.create_user(username, email, password)
user.save()
return HttpResponseRedirect('/keenhome/accounts/login/')
else:
form = RegisterForm()
return render_to_response("polls/register.html", {'form':form}, context_instance=RequestContext(request))
#This is used for reinputting if failed to register
else:
form = RegisterForm()
return render_to_response("polls/register.html", {'form':form}, context_instance=RequestContext(request))
对象类是:
class LivingRoom(models.Model):
'''Living Room object'''
user = models.OneToOneField(User)
def __init__(self, temp=65):
self.temp=temp
TURN_ON_OFF = (
('ON', 'On'),
('OFF', 'Off'),
)
TEMP = (
('HIGH', 'High'),
('MEDIUM', 'Medium'),
('LOW', 'Low'),
)
on_off = models.CharField(max_length=2, choices=TURN_ON_OFF)
temp = models.CharField(max_length=2, choices=TEMP)
#signal function: if a user is created, add control livingroom to the user
def create_control_livingroom(sender, instance, created, **kwargs):
if created:
LivingRoom.objects.create(user=instance)
post_save.connect(create_control_livingroom, sender=User)
Django 错误页面通知错误信息:user=User.objects.create_user(用户名、电子邮件、密码)
和LivingRoom.objects.create(user=instance)
The Django error page notifies the error information:
user=User.objects.create_user(username, email, password)
and
LivingRoom.objects.create(user=instance)
我试图搜索这个问题,找到了一些案例,但仍然不知道如何解决它.
I tried to search this problem, finding some cases, but still cannot figure out how to solve it.
推荐答案
你做不到
LivingRoom.objects.create(user=instance)
因为您有一个 __init__
方法,它不以 user
作为参数.
because you have an __init__
method that does NOT take user
as argument.
你需要类似的东西
#signal function: if a user is created, add control livingroom to the user
def create_control_livingroom(sender, instance, created, **kwargs):
if created:
my_room = LivingRoom()
my_room.user = instance
更新
但是,由于 bruno 有 已经说过了,Django的models.Model
子类的初始化器最好不要管,或者应该接受*args
和**kwargs
匹配模型的元字段.
But, as bruno has already said it, Django's models.Model
subclass's initializer is best left alone, or should accept *args
and **kwargs
matching the model's meta fields.
所以,遵循更好的原则,你可能应该有类似的东西
So, following better principles, you should probably have something like
class LivingRoom(models.Model):
'''Living Room object'''
user = models.OneToOneField(User)
def __init__(self, *args, temp=65, **kwargs):
self.temp = temp
return super().__init__(*args, **kwargs)
注意 - 如果您没有使用 temp
作为关键字参数,例如LivingRoom(65)
,那么你必须开始这样做.LivingRoom(user=instance, temp=66)
或者如果您想要默认值 (65),只需 LivingRoom(user=instance)
即可.
Note - If you weren't using temp
as a keyword argument, e.g. LivingRoom(65)
, then you'll have to start doing that. LivingRoom(user=instance, temp=66)
or if you want the default (65), simply LivingRoom(user=instance)
would do.
这篇关于__init__() 得到了一个意外的关键字参数“用户"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!