Monday, July 2, 2012

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

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.


On Mon, Jul 2, 2012 at 8:48 PM, Sunny Nanda <snanda85@gmail.com> wrote:
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

On Monday, July 2, 2012 6:47:03 PM UTC+5:30, Nikhil Verma wrote:
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 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-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.



--
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