我的用户类型为1,患者为2,医生为2。现在,我需要将用户类型2创建为is_staff=true
,并在获得其许可的情况下分配给医生组。
class UserType(models.Model):
user =models.OneToOneField(User, on_delete=models.CASCADE)
USER_TYPE = (
(1, 'Patient'),
(2, 'Doctor'),
)
user_type = models.IntegerField(choices=USER_TYPE)
答案 0 :(得分:0)
尝试一下
<input type='text' value='5' data-val='5' onchange='updateValue(event)'>
答案 1 :(得分:0)
只要保存了UserType
对象并且user_type
为2,您就可以使用signals
将用户添加到特定组中。
from django.contrib.auth.models import Group
from django.db.models.signals import post_save
from .models import UserType
def add_user_to_doctors_group(sender, instance, **kwargs):
if instance.user_type == 2:
doctors = Group.objects.get(name='doctors')
doctors.user_set.add(instance.user)
instance.user.is_staff = True
instance.user.save()
post_save.connect(add_user_to_doctors_group, sender=UserType)