In my project I have two different types of users: teacher and student, each with their own profile data.
After searching for the best approach it seems the way to go forward is using multi-table inheritance:
class BaseProfile(models.Model):
user = models.OneToOneField(User)
profile = models.CharField (max_length=10, choices={'teacher', 'student'})
# other common fields
class Teacher(BaseProfile):
# teacher specific fields
class Student(BaseProfile):
# student specific fields
And in settings.py
: AUTH_PROFILE_MODULE
= myapp.BaseProfile
.
Now I want to implement the same functionalities as in django-profiles:
- create profiles
- edit profiles
- display profiles
I have a good idea how to do the edit and display part when I have the correct value in the field profile
of BaseProfile
.
The problem:
Now I want the creation of the profile to be done automatically (and in the right db: Teacher
or Student
) directly when a user is created by using a signal.
The field profile
should contain the value "student" when the user registers through the site via the registration form. The value should be "teacher" when the admin creates a new user through the admin interface.
Anyone an idea how I can accomplish this? Probably I need to write a custom signal, something like the below, and send it from the User Model, but didn't found a working solution yet:
def create_user_profile(sender, instance, request, **kwargs):
if request.user.is_staff:
BaseProfile(user=instance, profile='teacher').save()
else:
BaseProfile(user=instance, profile='student').save()
Other and better approaches are of course also welcome!
Thanks!