Setting fields allows you to specify exactly which fields are accessible using this serializer. The most common form that a DRF serializer will take is one that is tied directly to a Django model:Ĭlass ThingSerializer(serializers.ModelSerializer): Similarly, when a user accesses information via the API the relevant instances are fed into the serializer, which parses them into a format that can easily be fed out as JSON to the user. When a user submits information (such as creating a new instance) through the API, the serializer takes the data, validates it, and converts it into something Django can slot into a Model instance. The DRF serializer handles this translation. While your data will be represented as instances of your Model classes in your Python code, it needs to be translated into a format like JSON in order to be communicated over an API. ![]() Router: defines the URLs which will provide access to each viewsetĭjango models intuitively represent data stored in your database, but an API will need to transmit information in a less complex structure.Viewset: defines the functions (read, create, update, delete) which will be available via the API. ![]() ![]() Serializer: converts the information stored in the database and defined by the Django models into a format which is more easily transmitted via an API.Basic ArchitectureĪ DRF API is composed of 3 layers: the serializer, the viewset, and the router. Django Rest Framework (DRF) is a library which works with standard Django models to build a flexible and powerful API for your project.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |