Hi Amitesh,
Assuming you are using model forms in django without any customisation,
as UserProfile model does not have first_name, last_name and email field,
reading the first_name from cleaned_data is failing.
To solve it, you have to add 3 extra fields in the UserProfileForm
i.e. first_name, last_name and email, and pop them before saving the form.
also save these three fields on the user model.
*request.user.first_name = form.cleaned_data.pop('first_name')*
request.user.save()
form.save()
Cheers!
Regards,
Chetan Ganji
+91-900-483-4183
[email protected]
http://ryucoder.in
On Tue, May 5, 2020 at 10:32 PM 'Amitesh Sahay' via Django users <
[email protected]> wrote:
> Hello Chetan,
>
> I was doing some random test, so I put "User" there. It is not the part of
> the original code.
>
> Below is models.py
>
> *from django.db import models*
> *from django.urls import reverse*
> *from django.contrib.auth.models import User*
>
>
> *class UserProfile(models.Model):*
> * user = models.OneToOneField(User, on_delete=models.CASCADE)*
> * Photo = models.FileField(upload_to='documents/%Y/%m/%d)*
> * uploaded_at = models.DateTimeField(auto_now_add=True)*
> * dob = models.DateField(max_length=20)*
> * country = models.CharField(max_length=100)*
> * State = models.CharField(max_length=100)*
> * District = models.CharField(max_length=100)*
> * phone = models.CharField(max_length=10)*
>
> * def get_absolute_url(self):*
> * return reverse('profile', kwargs={'id': self.id
> <http://self.id>})*
>
> Kindly give me the modified code that you think would work.
>
> Regards,
> Amitesh
>
>
> On Tuesday, 5 May, 2020, 09:24:58 pm IST, Chetan Ganji <
> [email protected]> wrote:
>
>
> Hi Amitesh,
>
> If you post the models, then only someone will be able to give you exact
> solution.
>
> Couple of things I noticed.
> How is this even working??
> You have not defined User variable in the function??
> If its the default User model, how would passing it to UserProfile will
> help in your scenario??
> pr = UserProfile(User)
>
> You dont need this line as it is already available as request.user. Why
> do you want to fetch it again, when it is already available???
> *pr.user = User.objects.get(id=request.user.id <http://request.user.id/>)*
> data2 = get_object_or_404(User, user=request.user)
>
> In the *profile_page *view, you can use reverse relation on the user
> model.
>
> Regards,
> Chetan Ganji
> +91-900-483-4183
> [email protected]
> http://ryucoder.in
>
>
> On Tue, May 5, 2020 at 9:02 PM 'Amitesh Sahay' via Django users <
> [email protected]> wrote:
>
> I have a profile page where I am fetching data from two models. One from
> the default User model and another one is custom UserProfile.
>
> However, The data from the custom model is getting populated, but not the
> User model.
> Below are the two functions responsible for the whole procedure.
>
> *def userprofileview(request): # Authenticated user filling the form to
> complete the registration*
> * if request.method == 'POST':*
> * form = UserProfileForm(request.POST, request.FILES)*
> * if form.is_valid():*
> * pr = UserProfile(User)*
>
> * pr.user = User.objects.get(id=request.user.id
> <http://request.user.id>)*
> * pr.first_name = form.cleaned_data['first_name']*
> * pr.last_name = form.cleaned_data['last_name']*
> * pr.email = form.cleaned_data['email']*
>
> * pr.dob = form.cleaned_data['dob']*
> * pr.country = form.cleaned_data['country']*
> * pr.State = form.cleaned_data['State']*
> * pr.District = form.cleaned_data['District']*
> * pr.phone = form.cleaned_data['phone']*
> * pr.save()*
>
> * messages.success(request, f'Profile has been updated
> successfully')*
> * return redirect('/profile')*
> * else:*
> * messages.error(request, AssertionError)*
> * else:*
> * form = UserProfileForm()*
> * return render(request, 'authenticate\\bolo.html', context={'form':
> form})*
>
>
> *@login_required*
> *def profile_page(request): # Fetching data from DB to show user's
> complete profile page*
> * data = get_object_or_404(UserProfile, user=request.user)*
> * data2 = get_object_or_404(User, user=request.user)*
> * context = {'data': data, 'data2': data2}*
> * return render(request, 'authenticate\\profile.html', locals())*
>
> To test it further, I realized that the three fields (email, first_name,
> and last_name) is coming from the "user". So, in the *"userprofileview" *I
> added* "user" *for those fields in the below format.
>
> pr.user.first_name = form.cleaned_data['first_name']
> pr.user.last_name = form.cleaned_data['last_name']
> pr.user.email = form.cleaned_data['email']
>
> However, I started to get below error
>
> -------------------------------------------------
> Internal Server Error: /fetch_data/ * # HTML Template*
> Traceback (most recent call last):
> File "C:\Python38\lib\site-packages\django\core\handlers\exception.py",
> line 34, in inner
> response = get_response(request)
> File "C:\Python38\lib\site-packages\django\core\handlers\base.py", line
> 115, in _get_response
> response = self.process_exception_by_middleware(e, request)
> File "C:\Python38\lib\site-packages\django\core\handlers\base.py", line
> 113, in _get_response
> response = wrapped_callback(request, *callback_args, **callback_kwargs)
> File "C:\Users\anshu\djago-project\AUTHENTICATION\views.py", line 65, in
> userprofileview
>
> * pr.user.first_name = form.cleaned_data['first_name']KeyError:
> 'first_name'*
> *--------------------------------------------------*
>
> It would be very kind of anybody who can help me rectify the issue. I have
> been struggling for 4 days minimum now.
>
> Amitesh
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/710931514.1164631.1588692639153%40mail.yahoo.com
> <https://groups.google.com/d/msgid/django-users/710931514.1164631.1588692639153%40mail.yahoo.com?utm_medium=email&utm_source=footer>
> .
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMKMUjvsJQ-7Zyh4t4x82pfA1KJYKbu_PtpSw%2BqiG7x38JDF4g%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAMKMUjvsJQ-7Zyh4t4x82pfA1KJYKbu_PtpSw%2BqiG7x38JDF4g%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2041909068.1176186.1588698102511%40mail.yahoo.com
> <https://groups.google.com/d/msgid/django-users/2041909068.1176186.1588698102511%40mail.yahoo.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAMKMUjvpcP%2BNsduU4ryeGaatUyU%3D-OPM1R6Wt6WMZz_rBrVWKA%40mail.gmail.com.