mercredi 1 mai 2019

Django Rest Framework JSONAPI and sideloaded/included resources

I am using the Django Rest Framwork JSON API for my Ember back end.

The (data) response I am getting back includes the "relationship" key but I need to sideload resources for a particular model, and hence want to include the "included" key as shown on the Ember docs https://guides.emberjs.com/release/models/relationships

I have a Product model with a FK relationship to a Tax model.

Here is my tax serializer:

from rest_framework_json_api import serializers
from .models import Tax

class TaxSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tax
        fields = ('id', 'name', 'amount')

Here is my product serializer:

from rest_framework_json_api import serializers
from .models import Product
from tax.serializers import TaxSerializer

included_serializers = {
    'tax': TaxSerializer
}

class Meta:
    model = Product
    fields = ('id', 'name', 'image', 'price','tax')

class JSONAPIMeta:
    included_resources = ['tax']

For this I've followed the example from https://www.mattlayman.com/blog/2017/sideload-json-api-django/

However, my response still includes the "relationships" key, and not the "included" key eg

"data" : [
   {
     "type":"products",
     "id": "1",
     "attributes": {...omitted for brevity ...
     },
   "relationships": {
      "tax": {
         "data": {
             "type":"tax",
             "id":"1"
          }
       }
    }
  },
  {...etc....}
]




Aucun commentaire:

Enregistrer un commentaire