Reputation: 2123
The employees field is a list of users which are employees of the user. Every user can have every user as employee, a user can also be an employee and have employees.
I have a problem where it automatically adds the user as an employee to all the employees added to the user. So when you add user 2 as employee to user 1, it also adds user 1 as employee of user 2.
How can I make it so it doesn't change other records? I simply want to add employees to the user being added.
I'm using Django REST 2.2. The employees field is a ManyToMany field, is this correct for my usage?
users/models.py
class MyUserManager(BaseUserManager):
def create_user(self, email, password=None):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser):
username = None
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_employee = models.BooleanField('is_employee', default=False)
employees = models.ManyToManyField("self", related_name='employees', blank=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = MyUserManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(selfself, app_label):
return True;
users/serializers.py
class CustomUserSerializer(serializers.ModelSerializer):
class Meta:
model = CustomUser
fields = ('id', 'email', 'password', 'is_employee', 'employees')
def create(self, validated_data):
validated_data['password'] = make_password(validated_data.get('password'))
return super(CustomUserSerializer, self).create(validated_data)
def update(self, instance, validated_data):
instance.email = validated_data.get('email')
instance.set_password(validated_data.get('password', instance.password))
instance.is_employee = validated_data.get('is_employee')
instance.employees.set(validated_data.get('employees'))
instance.save()
return instance
Upvotes: 0
Views: 91
Reputation: 10237
ManyToMany
relation is wrong for your case. Cause a user can't be an employee of several employers, can only be an employee of a single employer.
So, you should simply create a many-to-one relationship:
class CustomUser(AbstractBaseUser):
...
employer = models.ForeignKey('self', related_name='employees', on_delete=models.SET_NULL)
So, each user will have a single employer
field, and employees
set. And you can probably get rid of is_employee
field since you can check if a user is an employee by just checking if employer
field is set.
Upvotes: 0