Skip to content

JSONRenderer does not work when no pk is provided #722

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
mdettelson opened this issue Oct 9, 2019 · 2 comments · Fixed by #1127
Closed

JSONRenderer does not work when no pk is provided #722

mdettelson opened this issue Oct 9, 2019 · 2 comments · Fixed by #1127

Comments

@mdettelson
Copy link

mdettelson commented Oct 9, 2019

Hi there!

I have a particular use case here: I have a denormalized table that contains one data type (let's call it 'pieces') that gets aggregated up to another model (let's call them 'pies').

Since I can't do anything with pies without having all of the pieces (technical requirement), I've decided to keep a table representing only the pieces and aggregate up to the pie level when pies are asked for.

I noticed this line in the renderer code:

('id', encoding.force_text(resource_instance.pk) if resource_instance else None),
It's in the method build_json_resource_obj

Since my dataset has many repeats of the pie IDs, I can't use it as a pk, BUT once I aggregate the table the Pie Viewset, it IS unique. Since it is not the actual pk on the model, however, I get the error 'dict' object has no attribute 'pk'.

The ideal situation would be that I can set the pie ID as the id in the JSON API representiation.

Is there a built-in way to handle this sort of use case?

@sliverc
Copy link
Member

sliverc commented Oct 12, 2019

I am not 100% sure about your use case. But when using a ModelSerializer this is currently not possible: look at discussion #155 But you could also use a python model and overwrite pk property see discussion at #704

Closing this as duplicate. If you think it is not a duplicate please comment or otherwise continue the discussion in the other issue if needed. Thanks.

@sliverc sliverc closed this as completed Oct 12, 2019
@mdettelson
Copy link
Author

Ah ok, good to know! Thanks for the references.

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 a pull request may close this issue.

2 participants