__init__() got an unexpected keyword argument 'user'
Solution 1
You can't do
LivingRoom.objects.create(user=instance)
because you have an __init__
method that does NOT take user
as argument.
You need something like
#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
Update
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)
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.
Solution 2
I got the same error.
On my view I was overriding get_form_kwargs() like this:
class UserAccountView(FormView):
form_class = UserAccountForm
success_url = '/'
template_name = 'user_account/user-account.html'
def get_form_kwargs(self):
kwargs = super(UserAccountView, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
But on my form I failed to override the init() method. Once I did it. Problem solved
class UserAccountForm(forms.Form):
first_name = forms.CharField(label='Your first name', max_length=30)
last_name = forms.CharField(label='Your last name', max_length=30)
email = forms.EmailField(max_length=75)
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
super(UserAccountForm, self).__init__(*args, **kwargs)
Solution 3
LivingRoom.objects.create()
calls LivingRoom.__init__()
- as you might have noticed if you had read the traceback - passing it the same arguments. To make a long story short, a Django models.Model
subclass's initializer is best left alone, or should accept *args and **kwargs matching the model's meta fields. The correct way to provide default values for fields is in the field constructor using the default
keyword as explained in the FineManual.
noben
Updated on November 24, 2020Comments
-
noben over 3 years
i am using Django to create a user and an object when the user is created. But there is an error
__init__() got an unexpected keyword argument 'user'
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))
and the object class is:
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)
The Django error page notifies the error information:
user=User.objects.create_user(username, email, password)
andLivingRoom.objects.create(user=instance)
I tried to search this problem, finding some cases, but still cannot figure out how to solve it.
-
noben over 10 yearsYeah, the problem is from the
__init__()
function, but why this functiondoes NOT take user as argument
? -
shad0w_wa1k3r over 10 yearsBecause the function "init" has only 2 acceptable variables
def __init__(self, temp=65)
,self
(class instance) &temp
. Also, the class variable "user" is not defined for that instance as yet. -
ViaTech almost 5 yearsJust providing a note. I'm pretty sure if you were to call
super(UserAccountForm, self).__init__(*args, **kwargs)
beforeuser = kwargs.pop('user')
, there would be an error. I have been running into issues with this for a bit and eventually just started passing actual args into__init__
, but if you are saying this works, I'm feeling that is why.