I have an extended UserProfile model in django:
class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) #other things in that profile
And a signals.py:
from registration.signals import user_registered from models import UserProfile from django.contrib.auth.models import User def createUserProfile(sender, instance, **kwargs): profile = users.models.UserProfile() profile.setUser(sender) profile.save() user_registered.connect(createUserProfile, sender=User)
I make sure the signal gets registered by having this in my __init__.py
:
import signals
So that should create me a new UserProfile for every user that registers, right? But it doesn't. I always get "UserProfile matching query does not exist" errors when I try to log in, which means that the database entry isn't there.
I should say that I use django-registration, which provides the user_registered signal.
The structure of the important apps for this is, that I have one application called "users", there I have: models.py, signals.py, urls.py and views.py (and some other things which shouldn't matter here). The UserProfile class is defined in models.py.
Update: I changed the signals.py to:
from django.db.models.signals import post_save from models import UserProfile from django.contrib.auth.models import User def create_profile(sender, **kw): user = kw["instance"] if kw["created"]: profile = UserProfile() profile.user = user profile.save() post_save.connect(create_profile, sender=User)
But now I get a "IntegrityError":
"column user_id is not unique"
Edit 2:
I found it. Looks like somehow I registred the signal twice. The workaround for this is described here: http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemittedtwiceforeachsave
I had to add a dispatch_uid, now my signals.py looks like this and is working:
from django.db.models.signals import post_save from django.contrib.auth.models import User from models import UserProfile from django.db import models def create_profile(sender, **kw): user = kw["instance"] if kw["created"]: profile = UserProfile(user=user) profile.save() post_save.connect(create_profile, sender=User, dispatch_uid="users-profilecreation-signal")
A user profile is a collection of settings and information associated with a user. It contains critical information that is used to identify an individual, such as their name, age, portrait photograph and individual characteristics such as knowledge or expertise.
To begin, add a relevant image and a memorable quote that relates to this user. Describe the attitude of the user, their knowledge and literacy levels. Define what task they need to do. Describe their pain points and motivations.
A user profile is a directory of stored user settings and information for the related user account. For example, a user profile may have the settings for the user's installed programs and operating system. A user profile stored on a home computer is often unique to the computer on which it is stored.
You can implement it using post_save on the user:
from django.db.models.signals import post_save from models import UserProfile from django.contrib.auth.models import User def create_profile(sender, **kwargs): user = kwargs["instance"] if kwargs["created"]: profile = users.models.UserProfile() profile.setUser(sender) profile.save() post_save.connect(create_profile, sender=User)
Edit:
Another possible solution, which is tested and works (I'm using it on my site):
from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save def create_profile(sender, **kwargs): user = kwargs["instance"] if kwargs["created"]: up = UserProfile(user=user, stuff=1, thing=2) up.save() post_save.connect(create_profile, sender=User)
You can get the extended profile to be created when first accessed for each user instead:
from django.db import models from django.contrib.auth.models import User class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) additional_info_field = models.CharField(max_length=50) User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
then use
from django.contrib.auth.models import User user = User.objects.get(pk=1) user.profile.additional_info_field
ref: http://www.codekoala.com/blog/2009/quick-django-tip-user-profiles/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With