My ember app expects the JSON response to contain a primary key result for the model ID. Rather than use integer IDs, I want to use slugs. I know that in the standard django framework you can do something like this:
from rest_framework import serializers
from .models import Page
class PageSerializer(serializers.ModelSerializer):
id = serializers.CharField(source='slug')
class Meta:
model = Page
fields = ('name','id',...etc )
However, this doesn't work when using the json api - in my response I still have something like
"data": [{
"type": "pages",
"id": "2",
"attributes": {
"name": "Some page name",
}]
When I want the "id" field to be something like "some-page-name" (the slug)
Is this not possible with the json api. For clarity the equivalent json api import above would be
from rest_framework_json_api import serializers
Many thanks
**** addition ****
To help clarify the problem I am facing, here is a serializer using the standard REST framework. All attributes shown below are included in the Page model.
from rest_framework import serializers
from .models import Page
class PageSerializer(serializers.ModelSerializer):
id = serializers.CharField(source='slug')
class Meta:
model = Page
fields = ('name','id')
The JSON response I get at http://localhost:8000/api/pages is as follows
[
{
"name": "Page 1",
"id": "page-1"
},
{
"name": "Page 2",
"id": "page-2"
},
{
etc
}
]
When I use the json api, doing the exactly the same, but obviously importing
from rest_framework_json_api import serializers
I am not able to change the id value in the same way, I have something like this when viewing http://localhost:8000/api/pages
{
"links": {
omitted for brevity ...
},
"data": [
{
"type": "pages",
"id": "1",
"attributes": {
"name": "Page 1"
}
},
{
"type": "pages",
"id": "2",
"attributes": {
"name": "Page 2"
}
},
]
'id'
infields
. – dirkgrotenslug
field, why not just pass that as field (and give it the label "id")? – dirkgroten