question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Json-api renderer breaking when paired with JWT packages (simple-jwt & drf-jwt)

See original GitHub issue

Ran into this issue today while setting up a backend -

line 479, in build_json_resource_obj ('id', encoding.force_text(resource_instance.pk) if resource_instance else None), AttributeError: 'dict' object has no attribute 'pk'

Found out that is occurring from this line of code located inside of renderers.py

('id', encoding.force_text(resource_instance.pk) if resource_instance else None),

link-to-code

in particular its the “resource_instance.pk” thats blowing up. This occurs whenever I’m using JWT packages simple-jwt or drf-jwt

If I change the line to

('id', encoding.force_text(resource_instance['user'].pk) if resource_instance else None),

Everything works. Ideally I’d like to avoid having to go down the route of forking, does anyone know how I could get past this? It seems the renderer is making an assumption about how the resource_instance should be returned from the JWT serializer.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sliverccommented, Sep 11, 2018

This serializer looks a bit odd because it returns user as data but doesn’t define field. If it wouldn’t need to be serialized it would need to be defined as RelatedField marked as read_only.

Also I just quickly tested it and cannot reproduce an error:

Test code:

    url = reverse('rest_login')

    data = {
        'data': {
            'attributes': {
                'password': 'password',
                'username': 'admin',
            },
            'type': 'loginViews'
        },
    }
    response = client.post(url, data=data)
    assert response.status_code == 200

This reproduces following output:

{'data': {'attributes': {'key': 'a1c94b5cfd5d503d95cafc5d08be8afa1c0dbbc7'},
          'id': 'a1c94b5cfd5d503d95cafc5d08be8afa1c0dbbc7',
          'type': 'loginViews'}}

Maybe overwriting serializer and marking user as related field might help you already.

Otherwise could you provide a PR potentially which reproduces this error within the json api example?

0reactions
sliverccommented, Apr 29, 2020

@ewelinabuturla This issue is old and even though your issue might look similar it might not be. And when I read through it seems that I was actually never able to reproduce this issue.

I am locking this conversation. But if your issue still exists best open a pull request with a reproducing test for your issue (can also be an example application or similar if this is easier).

Read more comments on GitHub >

github_iconTop Results From Across the Web

rest_framework_simplejwt package — Simple JWT 5.2.2.post4 ...
An authentication plugin that authenticates requests through a JSON web token provided in a request header without performing a database lookup to obtain...
Read more >
Profile for Django LLC - Linknovate
Break the API into 2 parts — Instead of sending item details in first API just ... 3.1 .1 drf-jwt == 1.13 .3...
Read more >
Setting up JWT Authentication - Thinkster.io
Rendering User objects​​ Create conduit/apps/authentication/renderers.py. There's nothing new or interesting happening here, so just read through the comments in ...
Read more >
Simple Index
abstract-rendering · abstractfactory ... ad-testing-packaging-cs207 · ad-udacitydistributions ... ak-djangorestframework-jsonapi · ak-gchartwrapper
Read more >
Customizing JWT response from django-rest-framework ...
For example: to customize simpleJWT response by adding username and groups,. enter image description here. Override the validate method in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found