drf-flex-fields
drf-flex-fields copied to clipboard
dot-notation not working
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"
}
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.
Ohh. Okay. Thanks for Good Library. 👍
@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?
@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.
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.
I am having the same problem. Hope that solves the problem.
Everybody wants it but nobody what to do anything :D
Is it still in limbo?