0
votes

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....}
]

Update:

I am now getting the included key back in the response which is great. However, the whole point of doing this was that in my Ember models I don't have to create explicit relationships... from the Ember docs

when the API returns a deeply nested, read-only object or array, there is no need to create multiple models with DS.attr('hasMany') or DS.attr('belongsTo') relationships. This could result in a potentially large amount of unnecessary code. You can access these objects in the template without transforming them. This can be done with DS.attr() (No attribute type).

I have this done in my Product model in Ember:

tax: DS.attr()

In my templates, assuming I already have a product instance I would expect to to be able to access product.tax.amount - but I can't.

1

1 Answers

0
votes

user serializer depth= 1 or 2

class AccountSerializer(serializers.ModelSerializer):
class Meta:
    model = Account
    fields = ('id', 'account_name', 'users', 'created')
    depth = 1