Dhiman
Dhiman

Reputation: 113

Unexpected keyword argument

Here is my model Model

from Account.models import User
from django.db import models

class Seller(models.Model):
    seller = models.OneToOneField(User, on_delete = models.CASCADE)
    email = models.EmailField(max_length = 90, unique = True)
    country = models.CharField(max_length = 60)
    phone = models.CharField(max_length= 20, unique = True, blank = True, null = True)
    address = models.TextField()
    zipcode = models.CharField(max_length = 10)

form

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from .models import Seller
from Account.models import User

class SellerCreationForm(UserCreationForm):
    name = forms.CharField(required = True)
    email = forms.EmailField(required = True)
    phone = forms.CharField(required = False)
    country = forms.CharField(required = True)
    address = forms.CharField(required = True)
    zipcode = forms.CharField(required = True)

    class Meta(UserCreationForm.Meta):
        model = User

    @transaction.atomic
    def save(self):
        user = super().save(commit=False)
        user.is_seller = True
        user.name = self.cleaned_data.get('name')
        user.save()
        seller = Seller.objects.create(user=user)
        seller.email = self.cleaned_data.get('email')
        seller.phone = self.cleaned_data.get('phone')
        seller.country = self.cleaned_data.get('country')
        seller.address = self.cleaned_data.get('address')
        seller.zipcode = self.cleaned_data.get('zipcode')
        seller.save()
        return user

views

from django.shortcuts import render, redirect
from django.views.generic import CreateView
from .forms import SellerCreationForm
from django.contrib.auth.forms import AuthenticationForm
from Account.models import User

class RegisterSeller(CreateView):
    model = User
    form_class = SellerCreationForm
    template_name = 'registration/registerseller.html'

    def form_valid(self, form):
        user = form.save()
        login(self.request, user)
        return redirect('/')

I tried to create a project with multiple users types and all users having different functionality. Everything in the code functions well but whenever I try to register a new user I get an error. And it says Seller() got an unexpected keyword argument 'user'

Upvotes: 1

Views: 1792

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

The ForeignKey from the Seller to the User has been named seller, not seller`. You thus can construct an object with:

seller = Seller.objects.create(
    seller=user,
    email=self.cleaned_data['email']
    phone = self.cleaned_data['phone']
    country = cleaned_data['country']
    address = self.cleaned_data['address']
    zipcode = self.cleaned_data['zipcode']
)

But perhaps it is better to rename the field to:

from Account.models import User
from django.db import models

class Seller(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    # ⋮

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.

Upvotes: 2

Related Questions