how to bind HTTP POST data to python object in Django Rest Framework?


I will give an example to better explain my question

my request:


<a href="http://localhost:8080/users/" rel="nofollow">http://localhost:8080/users/</a>

request body: (This gets posted)

{"name":"Matt", "salary":10000, "blog_url":"www.myblog.com", "dept_name":"ENG" } class CustomRequest(object): def __init__(self,name,salary,blog_url,dept_name): self.name=name self.salary=10000 self.blog_url=blog_url self.dept_name=dept_name


class myUser(models.Model): //fields -- username, salary class myUserProfile(models.Model): User=models.OneToOneField(user) blog_url=models.URLfield() dept_name=models.ForeignKey(Department) @apiview(['POST']) def createUser(customrequest): myuser=user(customrequest.name, customrequest.salary) myuser.save() myuser.userprofile.blog_url(customrequest.blog_url) myuser.userprofile.dept_name(customrequest.dept_name) myuser.save()

I have been most of REST services using Java JAX-RS API. In this framework, POST request body is automatically deserialized to the object that the method takes in( in the above example, it is customrequest). A developer can define an object with attributes that he is looking for in the POST request and then perform the business logic.

Now that we are thinking of migrating to Django, I am wondering if Django Rest Framework provides this kind of behavior out of box. If so, how would I do that? Please note, in the JAX-RS world, there is no need for a developer to write a serializer. All that is needed is the transfer object where the incoming JSON gets deserailzed into.

I assume in Django, both a serializer and a transfer object is needed to achieve the same purpose.


In DRF, you have two options:

<ul><li>either you use ModelSerializer and you get the model instance automatically</li> <li>or you use Serializer and you get the validated_data and do whatever you like with it</li> </ul>

The serializer is the same as what you call transfer object, in the sense that both define the data structure and that both will hold the deserialized values.

For models, it is required for persistence, but that is also required in JAX-RS, unless you use the same classes as ORM entities(which is a bad design). So in JAX-RS you will have, for example, CustomRequest and JPA CustomRequestEntity


  • How to Add Prefix in WebView URL?
  • Cannot do axios PUT request in Django REST+React framework - Error 403
  • django model and sqlite db creation bug
  • Django 1.6: Displaying a particular models Objects in another template
  • How to write Django REST Api without model to send a file using Requests
  • django queryset filter foreignkey
  • JavaFX TextArea appendText works in initialize but not elsewhere
  • API Versioning in SailsJS
  • Change Mapping of fields of CreateUserWizard control with membership provider asp.net 4.0
  • Why is this Exception?- The relationship between the two objects cannot be defined because they are
  • Firebase rules validation does not validate data
  • Have a problem with sending a gmail message to a new registerd user
  • asp.net, gettype() and fully qualified class names
  • Take data from different tables and display it in View Index in mvc4
  • Django pagination with next, previous, count
  • Implement a writable serializer for multilevel nested relationships in django rest framework
  • Entity framework - Foreign key - data annotation
  • Django ORM Left Join With GROUP BY and SUM
  • Move an object (element) one step up with Javascript
  • Using Select and where statement in Criteria
  • Getting data from database using HQL
  • How to merge two Request in Laravel
  • Django: ORDER BY DESC on FloatField puts null values top?
  • (Tcl/Expect) clear screen after exit
  • Django return user model id with L
  • Tomcat memory Leak
  • How to write order and limit within cakephp joins array
  • MongoError: Incorrect arguments
  • Django rest serializer Breaks when data exists
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • AngularJs get employee from factory
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Free memory of cv::Mat loaded using FileStorage API
  • Getting error when using KSoap library to consume .NET web services
  • Memory offsets in inline assembly
  • Turn off referential integrity in Derby? is it possible?
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize