Don't strictly require 'pk' in HyperLinkedRelatedField. · Issue #2745 · encode/django-rest-framework (original) (raw)

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.