All you have to do is add the following code to your models.py (or any other
module that gets loaded by django, for that matter):

# models.py
from django.db.models import signals

def create_user_profile(sender, instance, created, **kwargs):
     if created:
        UserProfile.objects.create(user=instance)

signals.post_save.connect(create_user_profile, sender=User)

(assuming your profile model is called UserProfile, of course).

When django loads models.py, it will call that connect() method which in
turn makes create_user_profile get called every time the sender model (User)
is saved (hence the 'post_save').

More on this at https://docs.djangoproject.com/en/dev/ref/signals/


Cheers,
André Terra

On Mon, Jul 11, 2011 at 2:09 PM, Brent <brentba...@gmail.com> wrote:

> I am having trouble with this line of the tutorial: "You need to
> register a handler for the signal django.db.models.signals.post_save
> on the User model, and, in the handler, if created=True, create the
> associated user profile."
>
> It seems I need to edit the User model. I am confused, though, because
> I am using the User model included in django.contrib, and I am very
> hesitant to edit files that are not in my project directory.
>
> Is this bad design to be using a mix of things I've written on my own,
> and things that were already included in Django? (i.e. should I write
> everything from scratch?)
>
> Thanks.
>
> On Jul 11, 9:54 am, Michał Sawicz <mic...@sawicz.net> wrote:
> > Dnia 2011-07-11, pon o godzinie 09:48 -0700, Brent pisze:
> >
> > > How do I use a foreign key, though? In other words, how do I tell my
> > > code to look at UserProfile rather than just user?
> >
> > https://docs.djangoproject.com/en/1.3/topics/auth/#storing-additional...
> >
> > --
> > Michał (Saviq) Sawicz <mic...@sawicz.net>
> >
> >  signature.asc
> > < 1KViewDownload
>
> --
> 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.
>
>

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