message below:
> What does cc.models.UserProfile looks like ?
class UserProfile(models.Model):
user = models.OneToOneField(User)
thing = models.CharField(max_length=200)
def __unicode__(self):
return unicode(self.user)
> This should be :
>
> ... except cc.models.UserProfile.DoesNotExist:
>
> As a general rule:
> * NEVER use a bare except clause unless you reraise the SAME exception
> in the except block.
> * always catch the most specific exception type.
Noted! Thank you for your guidelines.
> Given the above statement, I assume the "models.py" file you're
> talking about is not the one where you define your UserProfile class.
> If yes - you may have legitimate reasons to split this between
> different apps -, are you sure the models.py file where you set up
> this signal handler is correctly registered and imported ? (hint: is
> the app containing this models.py in your settings.INSTALLED_APPS ?)
It is the same models.py file with the UserProfile class. Is this the
incorrect way to set this up? I have a feeling I'm completely missing
something.
I have it registered under INSTALLED_APPS using the name of the
project I created with startproject (myproject for the sake of
example) and the name of the app via startapp:
...,
'myproject.cc',
...,
> FWIW, you can insert print statements (poor man logging) in your
> models.py file to make sure it's imported and check if the signal
> handler is executed:
>
> # wherever/models.py
> from django.db.models.signals import post_save
>
> print "in %s" % __name__
>
> def profile_handler(sender, **kwargs):
> """ Signal handler to deal with a save on the User model """
> try:
> p = sender.userprofile
> print "in %s : for user %s, profile %s already exists" %
> (__name__, sender, p)
> except cc.models.UserProfile.DoesNotExist:
> profile = cc.models.UserProfile(user=sender)
> profile.save()
> print "in %s : for user %s, created profile %s " % (__name__,
> sender, p)
> except Exception, e:
> print "in %s : for user %s, got unexpected exception %s" %
> (__name__, sender, e)
> raise
>
> print "in %s : connecting profile_handler to post_save signal" %
> __name__
> post_save.connect(profile_handler, sender=User)
> print "in %s : connected profile_handler to post_save signal" %
> __name__
Thanks! I didn't know you could do that - I thought it would be
gobbled up as invalid output by the http server so I hadn't tried.
> <OT>While we're at it, the pythonic convention for indentations is 4-
> spaces, no tabs</OT>
Thank you for this as well! I'm both new to Django and Python -
learning as I go.
--
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