Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between model fields(in django) and serializer fields(in django rest framework)

As we can validate the values using the conventional model field then why Django REST Framework contains its own serializer fields. I know that serializer fields are used to handle the converting between primitive values and internal datatypes. Except this, is there anything different between them.

like image 617
Henu Avatar asked Feb 01 '16 11:02

Henu


1 Answers

Well there is a ModelSerializer that can automatically provide the serializer fields based on your model fields (given the duality you described). A ModelSerializer allows you to select which models fields are going to appear as fields in the serializer, thus allowing you to show/hide some fields.

A field in a model, is conventionally tied to a data store (say a column in a database).

A DRF Serializer can exist without a Django model too, as it serves to communicate between the API and the client, and its fields can be in many forms that are independent from the model and the backing database, e.g. ReadOnlyField, SerializerMethodField etc

like image 93
bakkal Avatar answered Sep 30 '22 16:09

bakkal