Skip to content

Defer to lookup_field/custom id fields #409

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
wants to merge 4 commits into from
Closed

Defer to lookup_field/custom id fields #409

wants to merge 4 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Feb 4, 2018

Thsi PR allow support for custom pk #155

There are so many places where PK is assumed in
djangorestframework-jsonapi and so many places/ways to change IDs/URLs
in DRF itself, that getting coverage on all the edge cases will be quite
a lot of work. Some of that work may be better spent re-factoring
things to make the code easier to understand and reduce the number of
edge cases in the first place.

@ghost ghost changed the title Issue 155 Defer to lookup_field/custom id fields Defer to lookup_field/custom id fields Feb 4, 2018
TODO Test coverage

There are so many places where PK is assumed in
djangorestframework-jsonapi and so many places/ways to change IDs/URLs
in DRF itself, that getting coverage on all the edge cases will be quite
a lot of work.  Some of that work may be better spent re-factoring
things to make the code easier to understand and reduce the number of
edge cases in the first place.
@ghost ghost changed the base branch from develop to master February 4, 2018 23:40
@codecov-io
Copy link

codecov-io commented Feb 4, 2018

Codecov Report

Merging #409 into master will decrease coverage by 0.22%.
The diff coverage is 50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #409      +/-   ##
==========================================
- Coverage   91.75%   91.53%   -0.23%     
==========================================
  Files          55       55              
  Lines        2923     2941      +18     
==========================================
+ Hits         2682     2692      +10     
- Misses        241      249       +8
Impacted Files Coverage Δ
rest_framework_json_api/renderers.py 83.6% <50%> (-1.73%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e501b04...1b0e853. Read the comment docs.

Copy link
Collaborator

@mblayman mblayman left a comment

Choose a reason for hiding this comment

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

This looks pretty good. I'd minimally like to know what's up with the getattr that I listed before I merge this. Thanks!

@@ -111,7 +111,8 @@ def extract_relationships(cls, fields, resource, resource_instance):
relation_data.append(
OrderedDict([
('type', relation_type),
('id', encoding.force_text(related_object.pk))
('id', encoding.force_text(getattr(
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's happening with this getattr? If my understanding of getattr is correct, then something seems off here. Let's say fields.lookup_field = 'foo'. Wouldn't this getattr call be equivalent to related_object.pk.foo?

Copy link
Author

Choose a reason for hiding this comment

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

something went wrong when cherry-picking, sorted now

getattr(field, 'lookup_field', None) or
nested_resource_instance._meta.pk.name)
if instance_pk_name in nested_resource_data:
pk = nested_resource_data[instance_pk_name]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you change this to use a consistent name? Higher in this diff, the variable passed to force_text was _id. Now it's pk. I think we should pick one and stick with it.

@subhaminion
Copy link

@mblayman Please merge if you are okay with the changes.

@ghost
Copy link
Author

ghost commented Mar 27, 2018

any chance to merge this PR please?

Copy link
Member

@sliverc sliverc left a comment

Choose a reason for hiding this comment

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

I am picking this up from @mblayman

Thanks for your work. It looks good so far. I think a few things need to be done to get this ready for merging though.

Some comments I have added above and here some general points:

  • Some test cases need to be added (doesn't have to fully cover all cases but we at least need a test case which covers the new feature you are implementing so we know it works
  • Add documentation how this can be used

@@ -216,9 +223,15 @@ def extract_relationships(cls, fields, resource, resource_instance):
utils.get_resource_type_from_instance(nested_resource_instance)
)

if hasattr(field.child_relation, 'lookup_field'):
Copy link
Member

Choose a reason for hiding this comment

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

couldn't this code block be simplified if we add a lookup_field to ResourceRelatedField defaulting to pk?

relation_id = relation if resource.get(field_name) else None
lookup_field = getattr(field, 'lookup_field', None)
if lookup_field:
relation_id = getattr(
Copy link
Member

Choose a reason for hiding this comment

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

could the lookup field not also be a relation?

pk = None
elif 'id' in fields:
pk = fields['id'].get_attribute(resource_instance)
elif 'url' in fields:
Copy link
Member

Choose a reason for hiding this comment

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

Instead of 'url' api_settings.URL_FIELD_NAME should be used.

@sliverc
Copy link
Member

sliverc commented Oct 25, 2018

@Ola-t
There hasn't been any progress on this since quite a while so I am closing this PR. If you still want to work on this please comment and I can reopen. Or if someone else wants to work on this change feel free to open a new PR.

@sliverc sliverc closed this Oct 25, 2018
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.

6 participants