Hi,

you have to create new "User" object, assign values to it and then 
assign this object to profile_obj. Example:

from django.contrib.auth.models import User

new_user = User()
profile_obj = UserProfile()
new_user.username = 'someusername'
new_user.password = 'somepassword'
new_user.save()
profile_obj.user=new_user
profile_obj.save()

Steven Nien pisze:
> Hi all,
> I have built a model like this:
>
> class UserProfile(models.Model):
>     address = models.CharField(max_length=60)
>     birthday = models.DateField()
>     user = models.ForeignKey(User)
>
> I render the model by the ModelForm:
>
> class UserProfileForm(ModelForm):
>     class Meta:
>         model = UserProfile
>         exclude = ('user', )
>
> I am trying to create a "blank" profile for the current user, if the
> profile does not already exist.
> I have tried the following code but it didn't work.
>
> if request.user.is_authenticated():
>     username = request.user.username
>     try:
>         profile = request.user.get_profile()
>     except ObjectDoesNotExist:
>         profile_obj=UserProfile(user=username)
>         profile_obj.save()
>
> which I got a error message:
> Cannot assign "u'someone": "UserProfile.user" must be a "User"
> instance.
>
> Can someone help me?
> Many thanks.
>
> >
>   


--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to