7👍
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
The method get_profile() does not
create the profile, if it does not
exist. 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.
The signal they mention is not really documented django-style where they provide code examples, so I will create an example for you:
from django.db.models import signals
from django.contrib.auth.models import User
def create_userprofile(sender, **kwargs):
created = kwargs['created'] # object created or just saved?
if created:
BBB.objects.create(user=kwargs['instance']) # instance is the user
# create a BBB "profile" for your user upon creation.
# now every time a user is created, a BBB profile will exist too.
# user.BBB or user.get_profile() will always return something
signals.post_save.connect(create_userprofile, sender=User)
1👍
That’s ok, everything works. DoesNotExist: BBB matching query does not exist.
means there is no BBB (user profile) for this user (matching query, i.e. get me the user profile for this user).
Use the DoesNotExist
exception to assert whether a particular user has an associated user profile. When you create a BBB
instance which is related to user a
, you won’t get a DoesNotExist
exception.
- [Django]-Simple API's to play around with for Python/Django?
- [Django]-Python range() builtin function, erm… malfunctioning with Django
- [Django]-Django Caching – How do I set up my code to avoid duplicating cache logic?
- [Django]-Django queryset – Isn’t it possible to filter on a FloatField?