using Django Rest framework to serialize custom data types and return response
You don't really need the REST Framework for this. All you need is to define a serializer
class instead of the CustomResponse
that you have.
in serializers.py
from django.core.serializers.json import Serializer
class UserSerializer(Serializer):
def get_dump_object(self, obj):
mapped_object = {
'user_name': obj.first_name,
'salary': obj.salary,
'dept_name': obj.dept_name
}
return mapped_object
then in your views.py
from myapp.serializers import UserSerializer
def getNameandDept(request, salary):
users = User.objects.filter(salary__gt=salary)
serializer = UserSerializer()
return HttpResponse(serializer.serialize(users), mimetype='application/json')
Don't forget to define the salary
argument in your urls.py
url(r'^users/(?P<salary>\d+)$', views.getNameandDept, name='getNameandDept'),
PS. You absolutely can do this with the DRF as well. It is a basic GET
call (the filtering by salary has no effect on the serializer), so all you need to do there is define a ModelSerializer
subclass with just the three fields
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('first_name', 'salary', 'dept_name')
and then serialize the output (note the slightly different syntax)
serializer = UserSerializer(users)
return Response(serializer.data)
Related videos on Youtube
brain storm
Updated on September 17, 2022Comments
-
brain storm over 1 year
Most of the tutorials on Django Rest Framework explains using the Django models and doing CRUD operations. That is a
GET
request on user model returns the attributes of user object in JSON format if I use JSON serializer.I am designing my Django application to process a query and return response. For example, I provide a REST API to get the results of the following query
"Get me the user first name and department whose salary than XXX"
Here are my Django models:
class UserProfile(AbstractUser): age = models.PositiveIntegerField(_("age")) salary=models.PositiveIntegerField(_("salary")) AUTH_USER_MODEL = "profiles.UserProfile" User = get_user_model() class Department(models.Model): users=models.ForeignKey(User) dept_name = models.CharField(max_length=30)
Now I have the following DTO (Data transfer object):
class CustomResponse(object): def __init__(self, user_name, salary, dept_name): self.user_name = user_name self.salary = salary self.dept_name=dept_name
In my REST service implemented using DRF, I want the following
@api_view(['GET']) def getNameandDept(salary): users=User.objects.filter(salary__gt=salary) toreturn=[] for user in users: response=CustomResponse(user.first_name,user.salary,user.dept_name) to_return.append(response) return Response(to_return)
I am not sure what is the right way to implement the above, with the tools that Django rest framework provide.
I am expecting the response something like this
[{user_name:"matt", salary:"5000", dept_name:"ENG"},{user_name:"smith",salary:"4000", dept_name:"HR"}....]
Thanks
EDIT
I was hoping DRF provides out of box tool for this kind of serialization. I have been using JAX-RS API (jersey and RESTeasy) that does this serialization.
-
brain storm about 9 yearswhen you mention about using ModelSerializer in DRF, you are serializing the entire user object. That it serializes all the model's attributes. But I am interested only in certain attributes of user FOR this particular GET request. eg : salary, first_name, department. NOT other attributes.
-
Martin B. about 9 yearsyou can specify which fields you want in the ModelSerializer, and also rename them if necessary, see django-rest-framework.org/api-guide/serializers/…
-
Martin B. about 9 yearsPS. This utilization of DRF is exactly as out-of-the-box as using Django-internal serializer really.. the syntax is slightly different, the amount of code is the same. (with the exception that DRF has some nice decorators for request type etc)
-
Martin B. about 9 yearsyeah, both the Django-internal and the DRF serializers expect to be dealing with model objects and don't just serialize all attributes of something. tbh I don't see much difference between defining your DTO vs defining a serializer.
-
brain storm about 9 yearswell, in the latter case, you would need both DTO and a serializer. but I trying to say, can I get away with just DTO and the framework handles serialization for me (just like Java frameworks such as JAX-RS)
-
Martin B. about 9 yearsno you don't need the DTO, you just define the fields in the serializer. You can rename fields in the ModelSerializer or just use the standard serializer (assuming you don't want some nice model-related features anyway) and define the fields as in my Django serializer example.. it's really the same as having that DTO thing
-
brain storm about 9 yearsIn real world applications, using model serializer would increase the payload and just serializing what is expected from the endpoint is way our company approaches designing API. so most often, we use DTO in Java world.
-
brain storm about 9 years+1. for the discussion. I am going to accept your answer after a little bit of research on my end. Here is another question that I posted which you might have a good knowledge of stackoverflow.com/questions/28331554/…