How DRF APIView in two related tables do not associate update

problem

There are two tables, a product table, a table for the classification of goods stored in the table category_id classification table id, but did not declare the primary key.
When doing the front differential update, incoming merchandise category name, you need to find the back end id classification by product type name, an update is ,, therefore need to find a method to call when receiving a patch request django

Resolution process

Here Insert Picture DescriptionFind a patch point into the parent class method
Here Insert Picture Descriptionto find the patch methods, discovered a method called partial_update its parent class, continue to point into view
Here Insert Picture Descriptionfound that the final call or update method, just passed a boolean value of whether the patch. Into the update method
Here Insert Picture Descriptionhere before incoming request.data, the data can be modified, added category_id can, so we can rewrite the update method.

method one

Here Insert Picture DescriptionIn this case can be resolved.

Method Two

Last article , referred to in order to make the picture return address does not contain the domain name I rewrote get_serializer_context () method, and now rewrite the update method, update method can be found
Here Insert Picture Descriptionget_serializer () method, the points go
Here Insert Picture Descriptionwe can directly remove kwargs modify data, solve, then the assignment commented context, you can kill two birds with one stone. It should be noted here that the dictionary does not need to directly modify the copy () then modify the
Here Insert Picture Descriptionperfect solution, you can modify the incoming class name
Here Insert Picture Description

Released three original articles · won praise 2 · views 60

Guess you like

Origin blog.csdn.net/qq_38727902/article/details/104797447