yezyilomo / django-restql

Turn your API made with Django REST Framework(DRF) into a GraphQL like API.
https://yezyilomo.github.io/django-restql
MIT License
620 stars 43 forks source link

Add documentation about supporting both add and create operations for many=False (one-to-many) fields #187

Closed dmugtasimov closed 4 years ago

dmugtasimov commented 4 years ago

Having it configured:

    organization = NestedField(
        OrganizationSerializer,
        accept_pk=True,
        create_ops=[ADD, CREATE],
        update_ops=[])

I am able to assign existing organization. But when I try to create one with either:

    "organization": {
        "create": {
            "name": "mvEkjEwIWsmcvjjHGQjzlEUOThKoCQmnrGyiVmfZVAkLGUwYbnnVWhBBhxKGXsqPvRDrgJfzHxsWjlNXcVslnKMpiVhZMcnnkBPkblpudePVSppkijXTRNrpBdPqlVOmUHDuPpXMdtVIiGUosYAAxgVvCsRLfYXvUarTBbsUdTdyabAubtJRYhZIGYglkjeUlJXBpfmreroReJUIptRTuHjcaGMjpAyJIqOmmBQsBVTpaNSxUGfmbKqYQjFlDbR",
            "organizationType": 1,
            "npi": "",
            "addressLine1": "xLCqpNCSitljQIPSvASsywcBdDtNbgBHxaOkpZSEyosvuoYAwUxcZmIkTXWmaJfpMXmvLoHFnxYMFZTjRpWjfUXuXwJXfzitzPBoMjzGhXaevMzUtkAVXrwQ",
            "addressLine2": "",
            "city": "YtdAFiKHETbFrVnILLqaeWvQUpdNjcBhvNHnfLdAdVRrfUnqMpmpSPxMSoINVDkTnSohIZbpwhGcnRecVxzYjkHThgXdyDvSbDzBrFlhXSyNYsMhSOyzSyZIFeZRvzwbldcEExxQEitwpaolDmNZlUtoXRBSyYmKrwNIzqfoRKqqFNIEnTKLNScLpxdvQvRctTEkNYiC",
            "state": 3,
            "country": 19,
            "zipCode": "AjmDiXUCBE",
            "phone": "rBRWNhYuWlMYqtz"
        }
    }

or

    "organization": {
            "name": "mvEkjEwIWsmcvjjHGQjzlEUOThKoCQmnrGyiVmfZVAkLGUwYbnnVWhBBhxKGXsqPvRDrgJfzHxsWjlNXcVslnKMpiVhZMcnnkBPkblpudePVSppkijXTRNrpBdPqlVOmUHDuPpXMdtVIiGUosYAAxgVvCsRLfYXvUarTBbsUdTdyabAubtJRYhZIGYglkjeUlJXBpfmreroReJUIptRTuHjcaGMjpAyJIqOmmBQsBVTpaNSxUGfmbKqYQjFlDbR",
            "organizationType": 1,
            "npi": "",
            "addressLine1": "xLCqpNCSitljQIPSvASsywcBdDtNbgBHxaOkpZSEyosvuoYAwUxcZmIkTXWmaJfpMXmvLoHFnxYMFZTjRpWjfUXuXwJXfzitzPBoMjzGhXaevMzUtkAVXrwQ",
            "addressLine2": "",
            "city": "YtdAFiKHETbFrVnILLqaeWvQUpdNjcBhvNHnfLdAdVRrfUnqMpmpSPxMSoINVDkTnSohIZbpwhGcnRecVxzYjkHThgXdyDvSbDzBrFlhXSyNYsMhSOyzSyZIFeZRvzwbldcEExxQEitwpaolDmNZlUtoXRBSyYmKrwNIzqfoRKqqFNIEnTKLNScLpxdvQvRctTEkNYiC",
            "state": 3,
            "country": 19,
            "zipCode": "AjmDiXUCBE",
            "phone": "rBRWNhYuWlMYqtz"
    }

I get HTTP400 {"organization":["Incorrect type. Expected pk value, received dict."]}

On the other hand without accept_pk=True,. I get HTTP400 {"organization":{"nonFieldErrors":["Invalid data. Expected a dictionary, but got int."]}} with "organization":17. Or I HTTP400 {"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]}

dmugtasimov commented 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.

yezyilomo commented 4 years ago

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

dmugtasimov commented 4 years ago

@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 commented 4 years ago

@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.