Kane Blueriver
Kane Blueriver

Reputation: 4268

How to create MyUser instance from User instance?

I have a custom User model from AbstractBaseUser, and User model is not abstract. Then I created two models, Student and Teacher, from User.

Now I have some user are both teacher and student, so I tried something like this:

t = Teacher.objects.first()
Student(user_ptr=t.user_ptr).save()

But get error:

IntegrityError: duplicate key value violates unique constraint "users_username_key"
DETAIL:  Key (username)=() already exists.

username here is the same as it is in django.contrib.auth.models.User, which is not nullable.

Teacher model is used to store the Teacher Profile of a user mostly, and so is Student. But I also use Them in different login view like teacher = get_or_404(Teacher, args); login(teacher). And Student would have a totally different authentication method.

Teacher model have some different field like work_email=CharField(unique=True,...) and start_teaching_time, Student have student_id=CharField(unique=True) and primary_school_entering_time.

PS: I need User, Teacher and also Student for three different authorizations. So they all can not be abstract.

Upvotes: 0

Views: 289

Answers (3)

Shobhit Sharma
Shobhit Sharma

Reputation: 1609

I guess your models look something like this.

class User(...):
    username = models.Charfield(unique=True, ...)

class Teacher(User, models.Model):
    teacher_field1 = models.CharField(...)

class Student(User, models.Model):
    student_field1 = models.CharField(...)

So, if you were to see the structure in the database, it'd be something like

User Table:

| id | username | ...
| 1  | teacher  | ...
| 2  | student  | ...

Teacher Table:

| user_ptr_id | teacher_field1 | ...
|     1       |   sample_text  | ...

Student Table:

| user_ptr_id | teacher_field1 | ...
|     2       |   sample_text  | ...

At the database level this structure solves your problem but at the Django ORM level, this won't work. So, in order to have a user both as a teacher and as a student, you'd need to have a OneToOneField of the user model in both Student and Teacher model and remove the inheritance.

Upvotes: 0

Sayse
Sayse

Reputation: 43300

Note: I'm assuming there isn't anything unique about a teacher/student.

If the above assumption is correct, then there isn't any reason to separate these two types of user into separate models.

All you really need to do is provide Many to many relationships for students and teachers on your course models between your User class and the course. (Since I assume a course can have multiple teachers/students and a student/teacher can teach/take multiple courses).


If they do have unique fields, then you may consider making separate UserProfile classes so a single user can have a teacher profile, and a student profile.

Based on your edit, it would appear cleaner, to me, to provide a Teacher and student profile for a user, these are essentially just separate models with a OneToOneField back to the user model.

Upvotes: 0

user1755802
user1755802

Reputation: 91

I think the problem is that the User can only be one or the other. A User can be a Teacher, or a Student but they cannot be both simultaneously.

I would suggest making User non abstract and work from there. Then you will just have a relationship from User to Teacher and Student which you can manipulate as you like.

Upvotes: 1

Related Questions