Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'Profile' object is not callable

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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",
                 ........
        ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading