> Hi All,
>
> So, if I do the following from the shell (as spawned by manage.py):
> >>> import cc.models
What does cc.models.UserProfile looks like ?
> >>> from django.contrib.auth.models import User
> >>> u = User.objects.get(username__exact="duh3")
> >>> try:
>
> ... u.userprofile
> ... except:
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.
> ... profile = cc.models.UserProfile()
> ... profile.user = u
> ... profile.thing = "Test"
> ... profile.save()
> ...
(snip)
> 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()
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 ?)
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__
Running this with the builtin dev server should provide some useful
informations about your problem.
<OT>While we're at it, the pythonic convention for indentations is 4-
spaces, no tabs</OT>
HTH
--
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