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

Don't strictly require 'pk' in HyperLinkedRelatedField. #2745

Closed
bleib1dj opened this issue Mar 22, 2015 · 9 comments
Closed

Don't strictly require 'pk' in HyperLinkedRelatedField. #2745

bleib1dj opened this issue Mar 22, 2015 · 9 comments

Comments

@bleib1dj
Copy link
Contributor

A direct check on an object for a pk attribute seems to tightly couple the HyperLinkedRelatedField with the Django ORM. It seems to reduce the ability to utilize the HyperLinkedRelatedField with other backends such as DynamoDB, Neo4j, and potentially others. It can be circumvented by adding a pk value to these data stores but I was wondering if it would make sense to instead use a definable attribute that defaulted to pk but could be overwritten if necessary.

The use_pk_only_optimization function that is used for the PKOnlyObject optimization could also be used prior to the direct check. I'm not sure if this would generate expected behavior though, as the majority of users who do use the ORM would end up losing the check whenever they changed their lookup_field.

@tomchristie
Copy link
Member

'pk' is just the default. Use lookup_field=... if you want something else.

@bleib1dj
Copy link
Contributor Author

class HyperlinkedRelatedField(RelatedField):
    ...
    def get_url(self, obj, view_name, request, format):
        """
        ...
        """
        if obj.pk is None:
            return None

        lookup_value = getattr(obj, self.lookup_field)
        ...

Hey @tomchristie, I might be missing something but how would setting the lookup_field resolve the AttributeError you'd receive by the obj.pk call?

@tomchristie tomchristie reopened this Mar 23, 2015
@tomchristie
Copy link
Member

Okay, so that line should could be tweaked to...

if hasattr(obj, 'pk') and obj.pk is None

Would make sense.

@tomchristie
Copy link
Member

It'd also be worth checking for any other points in the relationships that do the same and also adjusting them.

@tomchristie tomchristie changed the title Requirement of pk in HyperLinkedRelatedField Don't strictly require 'pk' in HyperLinkedRelatedField. Mar 23, 2015
@tomchristie tomchristie added this to the 3.1.2 Release milestone Mar 23, 2015
@bleib1dj
Copy link
Contributor Author

Alright I'll put that together and submit a pull request. I had added self.id_field = kwargs.pop('id_field', 'pk') in the __init__ and then referenced that with a getattr(obj, self.id_field) in place of the obj.pk is None and that seemed to work as well. If you'd like that approach instead just let me know. Otherwise I'll look around for any additional items doing the same thing and replace with a hasattr check.

@tomchristie
Copy link
Member

👍

jpadilla added a commit that referenced this issue Apr 5, 2015
…rictly_related

Enhancement dont require pk strictly related #2745
@xordoquy xordoquy modified the milestones: 3.1.2 Release, 3.1.3 Release May 13, 2015
tinchodipalma added a commit to tinchodipalma/django-rest-framework that referenced this issue May 25, 2015
The following items had a wrong href value:

- Dont require pk strictly for related fields. (encode#2745, encode#2754)
- Restrict integer field to integers and strings. (encode#2835, encode#2836)
@xordoquy
Copy link
Collaborator

xordoquy commented Jun 1, 2015

The associated PR was merged and released in the 3.1.2 release.
Can someone confirm this ticket should be closed ?

@xordoquy xordoquy removed this from the 3.1.3 Release milestone Jun 1, 2015
@tomchristie
Copy link
Member

Closing the ticket unless someone can confirm that it shouldn't be :)

@xordoquy
Copy link
Collaborator

xordoquy commented Jun 1, 2015

Good point ;)

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

No branches or pull requests

3 participants