drf-flex-fields icon indicating copy to clipboard operation
drf-flex-fields copied to clipboard

dot-notation not working

Open rajagopalx opened this issue 6 years ago • 8 comments

Based on https://github.com/rsinger86/drf-flex-fields#dynamically-setting-fields,

Here are my serializers:

class AddressBookSerializer(FlexFieldsModelSerializer):
    class Meta:
        model = SellerAddressBook
        fields = ('id', 'address', 'city', 'state', )
class OrderSerializer(FlexFieldsModelSerializer):
    address = AddressBookSerializer(many=False)

    class Meta:
        model = Order
        fields = ('id', 'order_name', 'address',)

On Get, /orders/123/?fields=id,order_name,address.city

ACTUAL RESULT

{
  "id" : 123
  "order_name" : "Order Name",
  "address" : {
    "id" : "1",
    "address": "my add",
    "city": "my_city",
    "state": " my state"
}

EXPECTED RESULT

{
  "id" : 13322
  "order_name" : "Order Name",
  "address" : {
      "city": "my_city"
 }

rajagopalx avatar Mar 02 '18 07:03 rajagopalx

Ah yes, thanks for noting this. This does not currently work, but I will certainly look into adding support for it. The current assumption is that your request for sparse fields gets injected into a dynamic serializer that is defined in the expandable_fields property. Serializers set up in the conventional manner as in your AddressBookSerializer example do not receive this input. However, there are ways of doing this, such as looking to the serializer parent for the fields input.

rsinger86 avatar Mar 03 '18 22:03 rsinger86

Ohh. Okay. Thanks for Good Library. 👍

rajagopalx avatar Mar 05 '18 19:03 rajagopalx

@rsinger86 this is something I need. I will gladly work up a PR, but could you please give me some guidance on the best way to implement it?

cdrx avatar Jul 19 '18 14:07 cdrx

@rsinger86 checking in again 2.5 years later :) I'm still happy to work up a PR fixing this if you can give me a steer on where to look.

cdrx avatar Jan 29 '21 16:01 cdrx

Sure! I appreciate the bump.

I think you could work from the apply_flex_fields method to pass the correct values to the child fields: https://github.com/rsinger86/drf-flex-fields/blob/master/rest_flex_fields/serializers.py#L53

Normally, the expand, fields and omit options get set in the constructor: https://github.com/rsinger86/drf-flex-fields/blob/master/rest_flex_fields/serializers.py#L36

But in this case, I don't think that's possible so maybe you want to add an set_flex_options method to the FlexFieldsModelSerializer, which can be called from the parent serializer.

rsinger86 avatar Feb 01 '21 07:02 rsinger86

I am having the same problem. Hope that solves the problem.

by-Exist avatar May 16 '21 08:05 by-Exist

Everybody wants it but nobody what to do anything :D

russian-developer avatar Nov 08 '22 09:11 russian-developer

Is it still in limbo?

striveforbest avatar Apr 28 '23 18:04 striveforbest