I am trying to create a new endpoint that would return a joined result of 3 tables (without having the model class)
The 3 models that I am trying to join are: User, Institution and Site in order to display the users with the name of the site they belong to.
I have defined my own serializer as follows:
class DashboardSerializer(serializers.Serializer):
id = serializers.CharField(read_only=True)
email = serializers.CharField()
first_name = serializers.CharField()
last_name = serializers.CharField()
full_address = serializers.CharField()
country = serializers.CharField()
status = serializers.CharField()
role = serializers.CharField()
date_joined = serializers.DateTimeField()
institution_id = serializers.CharField()
site_id = serializers.CharField()
site_name = serializers.CharField()
def create(self, validated_data):
pass
def update(self, instance, validated_data):
pass
and the view set:
class DashboardViewSet(viewsets.ModelViewSet):
queryset = User.objects.all().select_related('institution_id').select_related('site_id')
serializer_class = DashboardSerializer
@api_view(['GET'])
def get(self, request):
if request.method == 'GET':
users = User.objects.all().select_related('institution_id').select_related('site_id')
serializer = DashboardSerializer(users, many=True)
return JsonResponse(serializer.data, safe=False)
The problem is that when hitting the endpoint I get:
Invalid field name(s) given in select_related: 'institution_id', 'site_id'. Choices are: institution
I am not sure if this is a problem with the query, serializer or even both. Any help would be greatly appreciated.