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

As we can check the values ​​using the standard model field, then why the Django REST Framework contains its own serializer fields. I know that serializer fields are used to handle conversions between primitive values ​​and internal data types. Other than that, there is something else between them.

+4
source share
3 answers

Well there is ModelSerializerone that can automatically provide serializer fields based on your model fields (given the duality you described). A ModelSerializerallows you to choose which model fields will be displayed as fields in the serializer, which allows you to show / hide some fields.

A field in a model is traditionally tied to a data warehouse (for example, a column in a database).

DRF Serializercan exist without the Django model, since it serves as a link between the API and the client, and its fields can be in many forms that are independent of the model and support database, for example. ReadOnlyField, SerializerMethodFieldetc.

+3
source

- , .
( , , )

Serializer - , .
( , , )

models.ForeignKey(User) ,

Int UserSerializer ( ), http link, api . username, , .

DRF,
, / .
, .

+3

.

Model fields are used in the database, i.e. when creating a diagram that is visible only to the developer.

while Serializer fields are used to display api to the client, also visible to the client.

+2
source

Source: https://habr.com/ru/post/1626847/


All Articles