gaqhosting.blogg.se

Custom serializer django rest framework
Custom serializer django rest framework





custom serializer django rest framework

Return HttpResponse(rialize(users), mimetype='application/json')ĭon't forget to define the salary argument in your urls.py url(r'^users/(?P\d+)$', views.getNameandDept, name='getNameandDept'),

custom serializer django rest framework

Then in your views.py from rializers import UserSerializer In serializers.py from import Serializer All you need is to define a serializer class instead of the CustomResponse that you have. You don't really need the REST Framework for this. I have been using JAX-RS API (jersey and RESTeasy) that does this serialization. I was hoping DRF provides out of box tool for this kind of serialization. I am expecting the response something like this I am not sure what is the right way to implement the above, with the tools that Django rest framework provide. Response=CustomResponse(user.first_name,user.salary,pt_name) In my REST service implemented using DRF, I want the following getNameandDept(salary): Now I have the following DTO (Data transfer object): class CustomResponse(object):ĭef _init_(self, user_name, salary, dept_name): Salary=models.PositiveIntegerField(_("salary"))ĭept_name = models.CharField(max_length=30) Here are my Django models: class UserProfile(AbstractUser):Īge = models.PositiveIntegerField(_("age")) "Get me the user first name and department whose salary than XXX" I am designing my Django application to process a query and return response.įor example, I provide a REST API to get the results of the following query

custom serializer django rest framework

That is a GET request on user model returns the attributes of user object in JSON format if I use JSON serializer. Most of the tutorials on Django Rest Framework explains using the Django models and doing CRUD operations.







Custom serializer django rest framework