Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Invalid detail route rendering when using results as field name #518

Closed
@jurecuhalev

Description

@jurecuhalev

I have a basic django app with the following structure:
(omitted a few other models to make it more readable)

# models.py
from django.contrib.postgres.fields import JSONField

class Recording(TimeStampedModel):
    title = models.CharField(max_length=255, blank=True, default="")
    user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    status = models.CharField(default="new", max_length=64)
    audio = models.ForeignKey("Audio", null=True, on_delete=models.SET_NULL)
    language = models.CharField(max_length=255, blank=True)
    results = JSONField(default=dict)

# serializers.py
from rest_framework_json_api import serializers
from .models import Recording
class RecordingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Recording
        fields = ("id", "language", "audio", "status", "results")
        read_only_fields = ("status",)

# views.py
class RecordingViewSet(viewsets.ModelViewSet):
    model = Recording
    serializer_class = RecordingSerializer
    queryset = Recording.objects.all().order_by("-id")
    
    # explicit renderer to show the problem
    renderer_classes = (renderers.JSONRenderer,)

# urls.py
router = routers.DefaultRouter(trailing_slash=False)
router.register("recording", RecordingViewSet)
urlpatterns = [
path("api/", include(router.urls)),
]

When I visit: /api/recording, I get a correct output structure:

{
  "links": {
    "first": "http://transkriptor.test/api/recording?page%5Bnumber%5D=1",
    "last": "http://transkriptor.test/api/recording?page%5Bnumber%5D=1",
    "next": null,
    "prev": null
  },
  "data": [
    {
      "type": "recording",
      "id": "16",
      "attributes": {
        "language": "",
        "status": "new",
        "results": {
          
        }
      },
      "relationships": {
        "audio": {
          "data": {
            "type": "audio",
            "id": "82"
          }
        }
      }
    },
    {
      "type": "recording",
      "id": "15",
      "attributes": {
        "language": "",
        "status": "new",
        "results": {
          
        }
      },
      "relationships": {
        "audio": {
          "data": {
            "type": "audio",
            "id": "81"
          }
        }
      }
    },
...
}

But when I try to view a single record: /api/recording/16, I get fallback JSON structure instead of JSON API:

{
  "data": {
    "id": 16,
    "language": "",
    "audio": {
      "type": "audio",
      "id": "82"
    },
    "status": "new",
    "results": {
      
    }
  }
}

If I remove results from serializer (such as: fields = ("id", "language", "audio", "status")), then I get expected result structure:

{
  "data": {
    "type": "recording",
    "id": "16",
    "attributes": {
      "language": "",
      "status": "new"
    },
    "relationships": {
      "audio": {
        "data": {
          "type": "audio",
          "id": "82"
        }
      }
    }
  }
}

I would expect one of two things to happen:
A) There would be exception triggered somewhere if DJA doesn't know how to correctly format the data
or B) The output would be the same between listing and detail view

Versions:

djangorestframework==3.9.0
djangorestframework-jsonapi==2.6.0
Django==2.1.3

I've used renderer_classes to remove the variability of any default renderers or request headers. I've experimented with appending format=vnd.api%2Bjson but it doesn't change the output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions