Home > Enterprise >  'Profile' object is not callable
'Profile' object is not callable

Time:12-09

TypeError at /accounts/signup/ 'Profile' object is not callable

views.py

def signup(request):

    if request.user.is_authenticated:
        return redirect("review:index")

    if request.method == "POST":
        signup_form = CustomUserCreationForm(request.POST)
        profile_form = ProfileForm(request.POST)

        if signup_form.is_valid() and profile_form.is_valid():
            user = signup_form.save()

            profile = profile_form.save(commit=False)
            Profile.objects.create(
                user=user,
                nickname=profile.nickname,
            )

            auth_login(
                request,
                user,
            )
            return redirect("review:index")
    else:
        signup_form = CustomUserCreationForm()
        profile_form = ProfileForm()

    context = {
        "signup_form": signup_form,
        "profile_form": profile_form,
    }

    return render(request, "accounts/signup.html", context)

models.py

class User(AbstractUser):
         .....

class Profile(models.Model):
    nickname = models.CharField(max_length=8, unique=True, null=True)
          .......

forms.py

class CustomUserCreationForm(UserCreationForm):
    class Meta:
        model = get_user_model()
        fields = [
            "username",
            "password1",
            "password2",
        ]

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile()
        fields = [
            "nickname",
                 ........
        ]

In the process of filling out the signup form, I wanted to put the nickname data in profile.nickname.

But 'Profile' object is not callable. A TypeError is raised.

CodePudding user response:

In your form, model = Profile() creates an object. You just need to provide a class:

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = [
            "nickname",
                 ........
        ]
  • Related