In my current implementation I can query for users by name. I would like however that the search results only contain users that do not appear as part of a Patient relationship for a certain therapist. Should I try to populate my user objects with their therapist? Or is there a way to filter users out that appear in Patient table with a certain therapist?
class User(AbstractUser):
is_therapist = models.BooleanField(default=False)
is_patient = models.BooleanField(default=True)
def __str__(self):
return self.email
class Patient(models.Model):
therapist = models.ForeignKey(User, related_name="patients", on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
complaints = models.TextField(blank=True, default="")
class Meta:
constraints = [
models.UniqueConstraint(name='unique_patient', fields=['therapist', 'user'])
]
class UserViewSet(viewsets.ModelViewSet):
serializer_class = UserSerializer
def get_queryset(self):
queryset = User.objects.all()
full_name = self.request.query_params.get('full_name', None)
therapist_id = self.request.query_params.get('therapist_id', None)
if full_name is not None:
queryset = User.objects.annotate(full_name=Concat('first_name', V(' '), 'last_name')).filter(full_name__icontains=full_name)
return queryset
edit: I am only interested in users (patients) who are not in a Patient relationship for the currently logged in user (therapist_id).