Search code examples
django-rest-frameworkdjango-serializerjson-apidjango-rest-auth

dj-rest-auth incompatible with Django ReST Framework JSON API?


In a Django (v3.1.2) ReST Framework (DRF v3.12.1) Application, I am using the Django ReST Framework JSON API (DJA) extension (v3.2.0) to consume and produce resources in JSON:API format (Content-Type application/vnd.api+json). This DJA extension provides its own JSON parser and renderer.

In addition, I chose dj-rest-auth (v1.1.2) for authentication resources. However, it seems to be incompatible with DJA: When I issue a login request in JSON:API format, like

{
    "data": {
        "type": "LoginView",
        "attributes": {
            "username": "user",
            "password": "pwd"
        }
    }
}

the server fails to render the response with the exception 'dict' object has no attribute 'pk' at ... python3.8/site-packages/rest_framework_json_api/renderers.py, line 502, in build_json_resource_obj. Debugging into the request, I find that DJA expects the serializer output serializer.instance to have a private key (pk) field, from which the DJA renderer derives metadata like the ID that must be present in a application/vnd.api+json resource.

I have two questions:

  1. Is this indeed an incompatibility between the two libraries, or am I simply combining them in the wrong way? Is there something I need to configure, or do I need to somehow extend the authentication models?
  2. If indeed there is a fundamental problem, is there some way to disable DJA for authentication resources? To me, it seems that DJA can only be activated for the entire Django server or not at all, but it cannot be activated for a specific Django app only. Is there a way to consume and return standard aplication/json content for the authentication endpoint?

Solution

  • For the record and to close this question: I did not find out what the problem was. However, the exact use case described above is now working flawlessly with the following combination of libraries:

    • Python 3.9
    • Django 3.1.2
    • Django REST Framework 3.12.1
    • Django REST Framework JSON API 4.0.0
    • DJ-REST-Auth 1.1.2