0
votes

I'm trying to create a project where I need user profiles.I have extended django default User. I am able create users successfully,but I'm not able to add data into profile model,

for example: I have created user an user 'Demouser', user is successfully created and I'm also able to login with created user.But next step is to updata data about 'Demouser' in profiles model,for that I have created register view and form but doesn't seem to work.

Forms.py file:

class ProfileForm(forms.ModelForm):

    class Meta:
        model = profiles
        exclude=(
        'id','Username','User',
        )

Models.py :

   class profiles(models.Model):
    class Meta:
        verbose_name_plural='Profile\'s'

        Username=models.OneToOneField(
            User,
            on_delete=models.CASCADE,
            unique=True,
            related_name='profile',
        )
        first_name=models.CharField(
            max_length=25,
        )
        last_name=models.CharField(
            max_length=25,
        )
        email_id=models.EmailField()
        previous_projects=models.TextField(
            null=True,
            blank=True,
        )

Views.py :

class ProfileEditView(views.View):
    def get(self,request,*args,**kwargs):
         if request.user.is_authenticated:
            return render(request,'editprofile.html',context={'form':ProfileForm}) 

        else:
            messages.success(request,('You must Login into system for access'))
            return redirect('profiles:Login')

    def post(self,request,*args,**kwargs):
        user=User.objects.get(username=request.user.username)
        print(user)
        form=ProfileForm(request.POST,instance =user)
        if form.is_valid():
            form.save()
            messages.success(request,('Profile Edited succesfully'))
            return render(
                request,
                'editprofile.html',
                context={
                    'form':ProfileForm
                }
            )

When I update the data using ProfileEditView, suppose I update the First name of logged in User, The data is updated in default django User model , I want it to be updated in my profiles model...

thanks in advance

1

1 Answers

0
votes

One of the possible solution is to bring your profile instance and save your profile there. What i want to say from your post method

 def post(self,request,*args,**kwargs):
        user=User.objects.get(username=request.user.username)
        print(user)
        form=ProfileForm(request.POST)
        if form.is_valid():
            // profile is valid now bring profile instance
            profile = Profile.objects.get(username=user)
            profile.first_name = form.cleaned_data['first_name']
            profile.save()
            messages.success(request,('Profile Edited succesfully'))
            return render(
                request,
                'editprofile.html',
                context={
                    'form':ProfileForm
                }
            )