When a ModelSerializer is nested, it loses its custom defined fields #8354
-
I am using DRF's ModelSerializer and Nested relationships. Problem: The ModelSerializer, ProcedureSerializer when displayed shows all the custom fields defined on it correctly. However, when ProcedureSerializer is nested into customerSerializer, the fields of ProcedureSerializer shown are only those original fields defined in its model, and it loses the custom defined fields. The issue is that in nested modelserializer, some of the fields defined on, and visible on listing the nested serializer do not show up when listing the parent. Models:
Serializers:
If I display a procedure instance:
But, if I try to view the serializer customerSerializer which nests ProcedureSerializer, all the fields of ProcedureSerializer do not show up:
Thus, it is noted that two of those fields which are there in ProcedureSerializer, including eventtime (which is actually a model method) and finding_proc (which is a nested serializer) are missing from ProcedureSerializer.
What is happening here? Why are the fields being lost from the nested ModelSerializer? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, I think it it because you setup the procedure field in the Meta class instead of the main class. Moving it outside works : |
Beta Was this translation helpful? Give feedback.
Hi,
I think it it because you setup the procedure field in the Meta class instead of the main class. Moving it outside works :