right have following serializer:
class shiftserializer(serializers.modelserializer): confirmed_location = locationserializer(required=false) class meta: model = shift
when create new shift
object can pass along basic object this...
{date_requested: "2015-06-11", employee: 4, shift_requested: 2}
...and object gets created without issue.
in effort make shift
objects bit more verbose requested them, declared serializer employee
field gets include information employee:
class shiftserializer(serializers.modelserializer): confirmed_location = locationserializer(required=false) employee = employeeserializer(required=false) class meta: model = shift
that produced desired effect: can see requested shift without making second query server every single user. unfortunately, after doing started getting http 400 errors whenever tried post same object make new shift
:
invalid data. expected dictionary, got int.
i'm positive employeeserializer
freaking out because i'm not passing in entire employee
object, id
pointing particular employee
. possible include entire employee
object when get
ing shift
objects still accept single integer when post
ing create new shift
object?
to read-only representation of employee, can add serializermethodfield
. leave employee
field intact post
, put
requests , add serialized representation @ employee_data
.
class employeeserializer(serializers.modelserializer): class meta: model = employee class shiftserializer(serializers.modelserializer): confirmed_location = locationserializer(required=false) # method named get_employee_data employee_data = serializers.serializermethodfield() class meta: model = shift fields = ( 'confirmed_location', 'date_requested', 'shift_requested', 'employee', 'employee_data', ) def get_employee_data(self, obj): """ returns serialized representation of employee. """ return employeeserializer(obj.employee).data
Comments
Post a Comment