The only issue with this approach is that you're going to do an extra
db query every time an existing User object is modified.  Whenever a
user signs in, edits their email address, etc.  This may or may not be
a big deal depending on your app.

Instead, you might want to use the pre_save signal and check for the
existence of the id to see if it's been created yet or not.  Something
like this:

def create_profile_for_user(sender, instance, signal, *args,
**kwargs):
        if not instance.id:
                profile = Profile( user = instance )
                profile.save()

dispatcher.connect(create_profile_for_user, signal=signals.pre_save,
sender=User)

There's a closed ticket for adding a flag to let you know if the
object was created or not, but I don't think this is included in the
trunk yet.  Does anyone know when it will be?
http://code.djangoproject.com/ticket/4879

Udi
http://breasy.com/blog/



On Dec 8, 7:44 am, Alex Koshelev <[EMAIL PROTECTED]> wrote:
> Use django signals infrastructure. Handle post_save and post_delete
> signal and add or delete profile. Example of creation:
>
> from django.contrib.auth.models import User
> from django.db.models import signals
> from django.dispatch import dispatcher
>
> def create_profile_for_user(sender, instance, signal, *args,
> **kwargs):
>     from myapp.models import Profile
>     try:
>         Profile.objects.get( user = instance )
>     except ( Profile.DoesNotExist, AssertionError ):
>         profile = Profile( user = instance )
>         profile.save()
>
> dispatcher.connect(create_profile_for_user, signal=signals.post_save,
> sender=User)
>
> On 8 дек, 15:41, Julien <[EMAIL PROTECTED]> wrote:
>
> > Thanks for the link Thomas. In fact I had already stumbled on django-
> > profiles before. But I don't think this quite solve my problem.
>
> > I'd like the profile to be automatically created when a new user is
> > created, even when creating a user through the admin interface or
> > through the shell for example.
>
> > If the User class was a custom class of mine, I'd simply override the
> > save() and delete() methods. But since the User class is built in
> > Django, I don't know how to do to.
>
> > Any idea how to solve this?
>
> > Cheers!
>
> > Julien
>
> > On Dec 8, 10:52 pm, Thomas <[EMAIL PROTECTED]> wrote:
>
> > > Julien,
>
> > > have a look here:
>
> > >http://www.b-list.org/weblog/2007/nov/24/profiles/
>
> > > Thomas
>
> > > On Dec 8, 12:20 pm, Julien <[EMAIL PROTECTED]> wrote:
>
> > > > Hi all,
>
> > > > I feel like my question must have been asked before, but I couldn't
> > > > find any help on this group or through regular googling.
>
> > > > All I'm trying to do is that whenever a new user is created (either
> > > > through the admin interface, or with django-registration), an instance
> > > > of the associated profile class (AUTH_PROFILE_MODULE) is created too.
>
> > > > I guess I'd need to put a hook on the User.save() and delete()
> > > > methods, am I right? How could I do that?
>
> > > > Thanks a lot!
>
> > > > Julien
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to