@tom where do you stand now regarding this issue? Seems pretty odd to offer
model field names to the api user whereas they only see serializer field
names.
Just wondering if it moved along somewhere else or if I should start
implementing something custom on my side ;-)
Cheers,
John.
Le mercredi 4 février 2015 16:36:28 UTC+1, Antony Seedhouse a écrit :
>
> Excellent :)
>
> So far I have working method b) that involves building a mapping and using
> it to replace, which duplicates some of the code from
> remove_invalid_fields, so I am on the fence about whether it would be
> better/neater implemented directly in remove_invalid_fields; what do you
> think?
>
> class SerializerFieldOrderingFilter(OrderingFilter):
> def rewrite_to_source_fields(self, ordering, view):
> # Rewrite Serializer fields to their source fields
>
> serializer_class = getattr(view, 'serializer_class')
> if serializer_class is None:
> # Nothing to rewrite
> return ordering
>
> # get mapping of field_name to source
> source_field_mapping = {
> field_name: field.source or field_name for field_name, field in
> serializer_class().fields.items()
> }
>
> # rewrite to source field
> for k, field in enumerate(ordering):
> direction = '-' if field[0] == '-' else ''
> if field.lstrip('-') in source_field_mapping.keys():
> ordering[k] = direction +
> source_field_mapping[field.lstrip('-')]
> return ordering
>
> def filter_queryset(self, request, queryset, view):
> ordering = self.get_ordering(request)
>
> if ordering:
> # Rewrite Serializer fields as their source fields
> ordering = self.rewrite_to_source_fields(ordering, view)
> # Skip any incorrect parameters
> ordering = self.remove_invalid_fields(queryset, ordering, view)
>
> if not ordering:
> # Use 'ordering' attribute by default
> ordering = self.get_default_ordering(view)
>
> if ordering:
> return queryset.order_by(*ordering)
>
> return queryset
>
>
>
> On Wednesday, 4 February 2015 15:45:33 UTC+1, Tom Christie wrote:
>>
>> > Would it not be desired for this to work by default?
>>
>> Probably, yes.
>>
>
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.