vikrant vikrant - 7 months ago 10
Python Question

How to save edit profile in django

How can i save edited information in my Django.
I want to edit user profile and want to save them into my database.
for this i had made this method

@login_required()
def saveProfile(request):
if request.method =='POST':
print "heloo"
usr=request.POST['username']
first_name=request.POST['first_name']
last_name=request.POST['last_name']
email=request.POST['email']
mobile=request.POST.get('mobile_no')
exp=request.POST.get('experience')
skills=request.POST.get('Skills')
edu=request.POST.get('education')
linkedin=request.POST.get('linkedin_id')
print first_name
updated_values={'first_name': first_name}
Consultants.objects.filter(user__username=usr).update(skills=skills, user__first_name=first_name )

return HttpResponse("your account is updated succesfully")


Now when i execute the above code ,it saves the skills information but throws a error message
Consultants has no field named 'user__first_name
'
How can i access OneToOne model field

Answer

You cannot update two db tables in one update-statement. You have to update the Consultants (name it Consultant pls) and User models separately:

cons = Consultants.objects.filter(user__username=usr)
# update profile model
cons.update(skills=skills)  
# update user model
User.objects
    .filter(id__in=cons.values_list('user_id', flat=True))
    .update(first_name=first_name)

Beyond that, I recommend you to use a django model form for Consultant, add the fields for the User and override the save method in order to save the related User instance.