2π
β
Why you donβt try something more general and build your response separating the serializers like this (maybe you can use the same serializers in somewhere else):
def get(self, request, *args, **kwargs):
resp = {
'site': None,
'status': None,
'configuration': None,
'admin_settings': None,
}
sites = models.Site.objects.all()
resp['site'] = serializers.SitesSerializer(sites, many=True).data
admin_settings = models.AdminConfiguration.objects.all()
resp['admin_settings'] = serializers.AdminConfigurationSerializer(admin_settings, many=True).data
# and so
return Response(resp, status=status.HTTP_200_OK)
π€Oscar Gonzalez
1π
You can try it like this. This will also help to find out errors:
def get(self, request, *args, **kwargs):
resp = {
"site": None,
"status": None,
"configuration": None,
"admin_settings": None
}
sites = models.Site.objects.all()
resp['site'] = serializers.SitesSerializer(sites, many=True).data
if resp['site'].is_valid():
admin_settings = models.AdminConfiguration.objects.all()
resp['admin_settings'] = serializers.AdminConfigurationSerializer(admin_settings, many=True).data
if resp['admin_settings'].is_valid():
return Response(resp, status=status.HTTP_200_OK)
return Response(resp['admin_settings'].errors, status=status.HTTP_404_NOT_FOUND)
return Response(resp['site'].errors, status=status.HTTP_404_NOT_FOUND)
π€MD. SHIFULLAH
- [Django]-How painful is a django project deployment to a live (staging) site?
- [Django]-Show request.DATA in Django 500 error caused by Rest Framework
- [Django]-Django admin β OneToOneField inline throws "has no ForeignKey" exception
- [Django]-Django REST: What's the recommended approach for supporting nested collections in URLs, such as /users/5/snippets/
Source:stackexchange.com