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

send data or pk when accept_pk=True #190

Closed Mohamed-Kaizen closed 4 years ago

Mohamed-Kaizen commented 4 years ago

can i send data when accept_pk=True

class AddressSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
    """Serializer for address model."""

    class Meta:
        """Meta data."""

        model = models.Address
        fields = ("name", "country", "state")

class WareHouseSerializer2(DynamicFieldsMixin, NestedModelSerializer):
    """Serializer for warehouse model."""

    id = serializers.UUIDField(read_only=True)

    address = NestedField(AddressSerializer, accept_pk=True)

    remark = serializers.CharField(required=False)

    created_at = serializers.DateTimeField(read_only=True)

    updated_at = serializers.DateTimeField(read_only=True)

    class Meta:
        """Meta data."""

        model = models.WareHouse
        fields = "__all__"

views.py

class WareHouseViewSet(viewsets.ModelViewSet):
    """ViewSet for WareHouse model."""

    queryset = models.WareHouse.objects.all()

    serializer_class = serializers.WareHouseSerializer

    filter_backends = (OrderingFilter, SearchFilter)

    ordering = ("-created_at",)

    ordering_fields = ("created_at", "updated_at")

    lookup_field = "id"

when sending with pk it work

POST api/warehouse/

{
  "address": "c079d70e-eadd-4e20-b90f-2ecc6e28683b",
  "remark": "string",
  "code": "string",
  "name": "string",
  "is_bin_mandatory": true
}

it's work but when sending data of the address

POST api/warehouse/

  "address": {
    "name": "string",
    "country": "AF",
    "state": "string"
  },
  "remark": "string",
  "code": "string",
  "name": "string",
  "is_bin_mandatory": true
}

Response:

{
  "address": [
    "“{'name': 'string', 'country': 'AF', 'state': 'string'}” is not a valid UUID."
  ]
}

in other word drf-flex-fields let you do that , so is there way to do that with django-restql ?

yezyilomo commented 4 years ago

This is a good feature to have, someone requested it here(#187 ) too, we are definitely going to implement this.