Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - multiple profiles
    text
    copied!<p>In my project I have two different types of users: teacher and student, each with their own profile data.</p> <p>After searching for the best approach it seems the way to go forward is using multi-table inheritance:</p> <pre><code>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 </code></pre> <p>And in <code>settings.py</code>: <code>AUTH_PROFILE_MODULE</code> = <code>myapp.BaseProfile</code>.</p> <p>Now I want to implement the same functionalities as in django-profiles: </p> <ul> <li>create profiles</li> <li>edit profiles</li> <li>display profiles</li> </ul> <p>I have a good idea how to do the edit and display part when I have the correct value in the <code>field profile</code> of <code>BaseProfile</code>.</p> <p>The problem:</p> <p>Now I want the creation of the profile to be done automatically (and in the right db: <code>Teacher</code> or <code>Student</code>) directly when a user is created by using a signal. The field <code>profile</code> 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.</p> <p>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:</p> <pre><code>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() </code></pre> <p>Other and better approaches are of course also welcome!</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload