Reputation: 578
I'm using Django with Wagtail CMS and i created two proxy models from class User. I ant to add a group on user creation from the proxy classes. I tried to add signals but that apparently dose not work on proxy models.
So on save i would like to add teachers to Teachers
group and students to Students
#models.py
class User(AbstractUser):
class Types(models.TextChoices):
STUDENT = "STUDENT", "Student"
TEACHER = "TEACHER", "Teacher"
type = models.CharField(_('Type'), max_length=50, choices=Types.choices, default=Types.STUDENT)
class Student(User):
class Meta:
proxy: True
def save(self, *args, **kwargs):
if not self.pk:
self.type = User.Types.STUDENT
return super().save(*args, **kwargs)
class Teacher(User):
class Meta:
proxy: True
def save(self, *args, **kwargs):
if not self.pk:
self.type = User.Types.TEACHER
return super().save(*args, **kwargs)
Upvotes: 0
Views: 152
Reputation: 25227
You could do it at the end of the save
method:
class Student(User):
class Meta:
proxy: True
def save(self, *args, **kwargs):
creating = not self.pk
if creating:
self.type = User.Types.STUDENT
super().save(*args, **kwargs)
if creating:
self.groups.add(Group.objects.get(name='Students'))
Upvotes: 2