Error init got an unexpected keyword argument user

+1 vote

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) and LivingRoom.objects.create(user=instance).

How to solve it?

Jun 29, 2020 in Python by kartik
• 37,510 points
7,243 views

1 answer to this question.

0 votes

Hello @kartik,

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

Hope it helps!!
Thank you!

answered Jun 29, 2020 by Niroj
• 82,720 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

Python error "TypeError: range() integer end argument expected, got numpy.float64."

Hi @Kashish, try something like this: for i ...READ MORE

answered May 27, 2019 in Python by Harish
1,279 views
0 votes
1 answer

Error saying "TypeError: descriptor object needs an argument"

The error is pretty straight forward, toy ...READ MORE

answered May 28, 2019 in Python by Alisha
5,912 views
0 votes
1 answer

I got expected an indented block error.

Hi, You used one function in your code, ...READ MORE

answered Jun 16, 2020 in Python by MD
• 95,160 points
1,071 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,720 points
771 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,720 points
315 views
0 votes
1 answer

Error:Expected view to be called with a URL keyword argument named “pk”

Hello @kartik, View functions are called with the ...READ MORE

answered Jul 1, 2020 in Python by Niroj
• 82,720 points
1,607 views
0 votes
1 answer

Got an error while installing opencv-python

Hello @Nishant, The error you got itself suggest ...READ MORE

answered Sep 17, 2020 in Python by Niroj
• 82,720 points
3,189 views