How do I set multiple fields to a User as a ForeignKey ?
manager = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
tester = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
developer = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
designer = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
team_lead = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
I get :
ERRORS:
service_order.ServiceOrder.manager: (fields.E304) Reverse accessor ‘User.serviceorder_set’ for ‘service_order.ServiceOrder.manager’ clashes with reverse accessor for ‘service_order.ServiceOrder.db_user’.
It seems I have to give , related_name='manager'
manager = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, related_name='manager')
tester = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, related_name='tester')
developer = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, related_name='developer')
designer = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, related_name='designer')
team_lead = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, related_name='team_lead')
And, if you want your database tables normalized, you could implement it this way:
members = models.ManyToManyField(User, through=MemberRole)
with:
class MemberRole(models.Model):
team = models.ForeignKey('Team', on_delete=models.CASCADE) # Whatever the model is above
person = models.ForeignKey('User', on_delete=models.CASCADE)
role = models.ForeignKey('RoleName', on_delete=models.PROTECT)
class RoleName(models.Model):
name = models.CharField(max_length=20) # Optionally, primary_key=True