-1

[enter image description here][1]I don't know what is causing this error but i couldn't find any solution for this. i checked everything and everything seems to be fine but i don't know why this error is occuring.

Views.py

from django.contrib.auth import get_user_model
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView,FormView

from . import forms
# Create your views here.

def signup(request):
    if request.method =='POST':
        user_create_form = forms.UserCreateForm(data=request.POST)
        user_profile_form = forms.UserProfileInfoForm(data=request.POST)

        if user_create_form.is_valid() and user_profile_form.is_valid():

            user = user_create_form.save()
            user.save()

            profile = user_profile_form.save(commit=False)
            profile.user = user

            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']

            profile.save()
        else:
            print(user_create_form.errors,user_profile_form.errors)
    else:
        user_create_form = forms.UserCreateForm()
        user_profile_form = forms.UserProfileInfoForm()

    return render(request,'accounts/signup.html',{'user_create_form':user_create_form,
                                                    'user_profile_form':user_profile_form})

Models.py

from django.db import models
from django.contrib import auth

# Create your models here.

class User(auth.models.User,auth.models.PermissionsMixin):
    def __str__(self):
        return "@{}".format(self.username)

class UserProfileInfo(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)

    Contact_number =  models.IntegerField(blank=True)
    joined_at = models.DateTimeField(auto_now=True)

    profile_pic = models.ImageField(upload_to='profiles',blank=True)

    def __str__(self):
        return self.user.username + ' Profile'

Forms.py

from django.contrib.auth import get_user_model # this gets the model that is in the application
from django import forms
from django.contrib.auth.forms import UserCreationForm
from . import models

class UserCreateForm(UserCreationForm):

    class Meta():
        fields = ('username','email','password1','password2',)
        model = get_user_model()

    def  __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].label = 'Display Name' # to set up a custom label for the field
        self.fields['email'].label = "Email Address"

class UserProfileInfoForm(forms.ModelForm):

    class Meta():
        model = models.UserProfileInfo
        fields = ('Contact_number','profile_pic')

I am getting this error no matter what i do, i tried referencing other similar questions but couldn't find any solution for this error. pls help me out on this one.

Thanks in Advance !

image of the error [1]: https://i.stack.imgur.com/HOcmf.png

Rahul
  • 17
  • 6

1 Answers1

0

You can overwrite save() method in your model to return a model instance after saving an object, for example:

class YourModel(models.Model):
name = models.CharField(max_length=20)

def save(self, *args, **kwargs):
    super(YourModel, self).save(*args, **kwargs) 
    return self

your_model_saved_instance = YourModel(name='example').save()

Then you will receive an instance from the user class instead of the form class

user = user.save()
Ashrof
  • 146
  • 4
  • sorry but i don't get it, how will this solve this problem. and i tried this solution but i get back the same error – Rahul Dec 28 '22 at 16:48