Closed dmugtasimov closed 4 years ago
found this in the source code:
BaseClass = BaseReplaceableNestedField if accept_pk \
else BaseWritableNestedField
looks like add and create are not supported simultaneously for the same foreign key. It is true, please, state it explicitly in the documentation. But better fix it to be consistent with many-to-many relations which support add and create operations on the same field simultaneously.
You don't need create_ops
and update_ops
kwargs for a foreign key relation.
organization = NestedField(OrganizationSerializer, accept_pk=True)
create_ops
and update_ops
kwargs are used along with many=True
@yezyilomo thank you. Is it still going to accept dict (JSON object) to create an organization? Or I have to choose either pk or creation a new object?
@yezyilomo thank you. Is it still going to accept dict (JSON object) to create an organization? Or I have to choose either pk or creation a new object?
With create_ops
and update_ops
You can use both pk and create new object.
Check https://django-restql.yezyilomo.com/mutating_data/#using-nestedfield-with-create_ops-and-update_ops-kwargs for more details about using create_ops
and update_ops
.
Having it configured:
I am able to assign existing organization. But when I try to create one with either:
or
I get
HTTP400 {"organization":["Incorrect type. Expected pk value, received dict."]}
On the other hand without
accept_pk=True,
. I getHTTP400 {"organization":{"nonFieldErrors":["Invalid data. Expected a dictionary, but got int."]}}
with"organization":17
. Or IHTTP400 {"organization":{"addressLine1":["This field is required."],"city":["This field is required."],"zipCode":["This field is required."],"phone":["This field is required."],"name":["This field is required."],"organizationType":["This field is required."],"country":["This field is required."]}}
with"organization":{"add":[18]}