background
When using Django’s ORM function in the project, it is inevitable that serialization and deserialization are involved (for example: python objects are converted into byte sequences that can be used for network transmission; byte sequence data in HTTP requests are converted into python objects) . In the design of Django REST Framwork (hereinafter referred to as DRF), one of the missions of the Serializers class and its subclasses is to help us complete this conversion.
Problem Description
Use postman to call DRF’s REST interface, and the following error is reported when reading data:
`HyperlinkedIdentityField` requires the request in the serializer context. Add `context={ ' request ' : request}` when instantiating the serializer. "
problem solved
After repeated testing, it is found that in specific Serializers, fields need to specify specific model attributes:
Wrong code:
class SimulationConfigSerializer(serializers.HyperlinkedModelSerializer): . . . class Meta: model = SimulationConfig fields = " __all__ "
Correct code:
class SimulationConfigSerializer(serializers.HyperlinkedModelSerializer): . . . class Meta: model = SimulationConfig fields = ['id','simulation_name','create_time']