class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
2011/3/2 Jeremiah <wanderinweezard@gmail.com>:
> Hi All,
>
> I'm going through the help document (http://docs.djangoproject.com/en/
> 1.2/topics/auth/#storing-additional-information-about-users) and I'm
> starting to figure out how signals work. What I'm having problems
> with is getting my signal to trigger (or at least figuring out if the
> signal is actually triggering?)
>
> So, if I do the following from the shell (as spawned by manage.py):
>>>> import cc.models
>>>> from django.contrib.auth.models import User
>>>> u = User.objects.get(username__exact="duh3")
>>>> try:
> ... u.userprofile
> ... except:
> ... profile = cc.models.UserProfile()
> ... profile.user = u
> ... profile.thing = "Test"
> ... profile.save()
> ...
> <UserProfile: duh3>
>>>> u.userprofile.user
> <User: duh3>
>>>> u.userprofile.thing
> u'Test'
>
> I can get the profile to work. So, then I add the following lines to
> my "models.py" file:
> from django.db.models.signals import post_save
> ...
> def profile_handler(sender, **kwargs):
> """ Signal handler to deal with a save on the User model """
> try:
> sender.userprofile
> except:
> profile = cc.models.UserProfile()
> profile.user = sender
> profile.save()
>
> post_save.connect(profile_handler, sender=User)
>
> But, what i can't tell is if anything is happening. If I create a new
> user and then try userinstance.userprofile, I get the expected
> exception.
>
> Could someone please point me at my issue?
>
> Thanks,
> Jeremiah
>
> --
> 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.
>
>
--
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