Monday, July 2, 2012

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

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment