Skip to content

replaced '.' for '__' in dotted ordering sources #5525

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

Closed

Conversation

OmegaDroid
Copy link
Contributor

Description

I ran into an issue the other day where a serializer referring to a field in a related object didn't provide the correct field name for the ordering filter backend.

Updating the get_default_valid_fields method so that for dotted sources the . for __ fixes my use case.

@@ -199,7 +199,7 @@ def get_default_valid_fields(self, queryset, view, context={}):
raise ImproperlyConfigured(msg % self.__class__.__name__)

return [
(field.source or field_name, field.label)
(field.source.replace('.', '__') or field_name, field.label)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

source defaults to None, so I'm worried we'll get an error here.

>>> None.replace('.', '__')
AttributeError: 'NoneType' object has no attribute 'replace'

class OrderingDottedRelatedSerializer(serializers.ModelSerializer):
related_text = serializers.CharField(source='related_object.text')
related_title = serializers.CharField(source='related_object.title')

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need an additional field here not setting the source to test.

@carltongibson carltongibson added this to the 3.7.2 milestone Oct 25, 2017
@carltongibson
Copy link
Collaborator

@OmegaDroid I added a test covering my concern in #5533. tl;dr: no issue.

Thanks for the input! Great stuff.

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

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy