UserProfile.user" must be a "User" instance. django-registration

8,867 views
Skip to first unread message

Nikhil Verma

unread,
Jul 2, 2012, 9:17:03 AM7/2/12
to django...@googlegroups.com
HI All

I am applying an django-registration in my app. So i create a UserProfile Model and  a ModelForm after clicking on the email link
the user is redirected to the ModelForm page of UserProfile. I am filling the details and trying to save it, however i am getting the above error.


Traceback:-


Exception Type: ValueError at /myprofile/completingprofile/
Exception Value: Cannot assign "<django.utils.functional.SimpleLazyObject object at 0x3487c10>": "UserProfile.user" must be a "User" instance.


model

class UserProfile(models.Model):
    user = models.ForeignKey(User, blank=True, null=True, unique=True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30, blank=True)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    mobile = models.CharField(max_length=15, blank=True)
    primary_email = models.EmailField(max_length=60, blank=True)

    # For professional Account
    institution_name = models.CharField(max_length=100,blank=True,null=True)
  
    street = models.CharField(max_length=75)
    state = models.CharField(max_length=30)
    zip_code = models.IntegerField(max_length=7, blank=True, null=True)
    country = models.CharField(max_length=30, blank=True)
   
      
    def __unicode(self):
        name = self.first_name + self.last_name
        return name


form.py

class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfile
        exclude = ('user',)
       
    def __init__(self, *args, **kwargs):
        super(UserProfileForm, self).__init__(*args, **kwargs)



views.py

def completingprofile(request):
    """
    Creating Profile
    """
    print request
    if request.method == "POST":
       
        form = UserProfileForm(request.POST)
        if form.is_valid():
            userprofile_obj = UserProfile(   
                first_name = form.cleaned_data['first_name'],
                last_name = form.cleaned_data['last_name'],                               
                gender = form.cleaned_data['gender'],
                mobile = form.cleaned_data['mobile'],
                institution_name = form.cleaned_data['institution_name'],
                street = form.cleaned_data['street'],
                zip_code = form.cleaned_data['zip_code'],
                state = form.cleaned_data['state'],
                country = form.cleaned_data['country'],
                user = request.user,# here i am trying to add user from request who is coming from RegistrationForm from django -registration
               # I am getting the error in this above line 
            )
            userprofile_obj.save()
            logger.info("Save profile for user: %s" % request.user)
           
            return HttpResponseRedirect('/thanks/')
    else:
        form = UserProfileForm()
    return render_to_response("myprofile/profile_page.html",
        {"form": form },
        context_instance=RequestContext(request)
        )   
 
How can i save the user field ?


Thanks for help in advance

--
Regards
Nikhil Verma
+91-958-273-3156

megaBos

unread,
Jul 2, 2012, 10:01:29 AM7/2/12
to django...@googlegroups.com
it seems fine, don't know why it shouldn't work, a different approach would be to overide the models save method and add the user there.

Sunny Nanda

unread,
Jul 2, 2012, 11:18:45 AM7/2/12
to django...@googlegroups.com
Hey Nikhil,

You can check a couple of things here:

1. Have you made sure that the user is logged in? You can do this by adding @login_required decorator to the  completingprofile view. (This is the most likely reason)
2. Instead of ForeignKey, make user field as OneToOneField. (but I am pretty sure this does not cause any problem)
3. Check that AUTH_PROFILE_MODULE variable is set in your settings.

~Sunny

Nikhil Verma

unread,
Jul 2, 2012, 12:11:47 PM7/2/12
to django...@googlegroups.com
Hi Sunny


I am creating a sign up link in this case.

When i place @login_required decorator it directs me to home page.I am applying django-registration app.
After pre-registration mail is sent to the email address and then it redirects back (success_url) to this completingprofile.

AUTH_PROFILE_MODULE is correct.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/UmYdxv2-Mr8J.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Melvyn Sopacua

unread,
Jul 4, 2012, 8:27:24 AM7/4/12
to django...@googlegroups.com
On 2-7-2012 15:17, Nikhil Verma wrote:

> Traceback:-
>
>
> Exception Type: ValueError at /myprofile/completingprofile/
> Exception Value: Cannot assign "<django.utils.functional.SimpleLazyObject
> object at 0x3487c10>": "UserProfile.user" must be a "User" instance.
>

> user = request.user,# here i am trying to add user from
> request who is coming from RegistrationForm from django -registration
> # I am getting the error in this above line

If request.user.is_anonymous() is True you have your answer. If not,
figure out what kind of stuff is in that object by printing it's __dict__.
--
Melvyn Sopacua


曾阿牛

unread,
Jun 9, 2014, 11:14:33 AM6/9/14
to django...@googlegroups.com
I have the same problem.

Nikhil Verma於 2012年7月2日星期一UTC+8下午9時17分03秒寫道:

Jimish Parekh

unread,
Jun 10, 2014, 6:55:54 AM6/10/14
to django...@googlegroups.com
Hey Nikhil,
I might be wrong but please check your UserProfile model
  • In your __Unicode(something) function check for syntax error. i think it should be __Unicode__
  • And why are you trying to return full name of User. I think you should return self.user there so that when you are requesting back you can get User Object back
If these answers are wrong and feels like bullshit then pls ignore... I am also trying to learn:)
Thanks,

On Monday, July 2, 2012 6:47:03 PM UTC+5:30, Nikhil Verma wrote:
Reply all
Reply to author
Forward
0 new messages