Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dot-notation not working #12

Open
rajagopalx opened this issue Mar 2, 2018 · 8 comments
Open

dot-notation not working #12

rajagopalx opened this issue Mar 2, 2018 · 8 comments

Comments

@rajagopalx
Copy link

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"
 }

@rsinger86
Copy link
Owner

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.

@rajagopalx
Copy link
Author

Ohh. Okay. Thanks for Good Library. 👍

@cdrx
Copy link

cdrx commented Jul 19, 2018

@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
Copy link

cdrx commented Jan 29, 2021

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

@rsinger86
Copy link
Owner

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.

@by-Exist
Copy link

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

@russian-developer
Copy link

Everybody wants it but nobody what to do anything :D

@striveforbest
Copy link

Is it still in limbo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants