Another idea would be to connect to User's post-save signal. You can
set it up to check whether the User instance being saved is just being
created, and if so, you can create its UserProfile instance at the
same time. That'd make it so all future users have profiles as soon as
they're created; once you create UserProfile instances for each of
your existing users, you could probably avoid the check altogether.

Example:
from django.db.models.signals import post_save
from django.contrib.auth.models import User

def create_profile(sender, **kwargs):
    if 'created' in kwargs and 'instance' in kwargs:
        if kwargs['created']:
            profile = UserProfile(user=kwargs['instance'])
            profile.save()
post_save.connect(create_profile, sender=User)

HTH,
Justin

On Sep 11, 8:45 pm, darren <backdoc...@gmail.com> wrote:
> I think that my main problem was that I was expecting the save to the model
> form to actually create the profile.
>
> Here's what I ended up with in my view.
>
>  86 @login_required
>  87 def createProfile(request):
>  88     UserProfile.objects.get_or_create(user=request.user)[0]
>  89     if request.method == 'POST':
>  90         form = UserProfileForm(request.POST,
> instance=request.user.get_profile())
>  91         if form.is_valid():
>  92             form.save()
>  93             return HttpResponseRedirect("/")
>  94         else:
>  95             return render_to_response('fav/createProfile.tpl', { 'form'
> : form  }, RequestContext(request) )
>  96     else:
>  97         form = UserProfileForm(instance=request.user.get_profile())
>  98     return render_to_response('fav/createProfile.tpl', { 'form' : form}, 
> RequestContext(request))
>
>  99
>
> On Sat, Sep 11, 2010 at 12:37 AM, Shawn Milochik <sh...@milochik.com> wrote:
> > I think you just may be missing a call to get_profile() in this view.
> > You can just do that and do a try block with an except block for
> > DoesNotExist. That will let you know the situation you're in, whether
> > the profile already existed or not.
>
> > Also, unless I'm misreading something you're trying to pass a User
> > instance as the instance for a user profile, which will not work. If
> > anything, you should be doing a .get() on the user profile where user
> > = request.user, but that doesn't matter anyway because get_profile()
> > does the same thing and is the correct way to do this.
>
> > Shawn
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django-us...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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