- Notifications
You must be signed in to change notification settings - Fork302
Description
I ran into this issue when upgrading to a newer version ofdjango-rest-framework-json-api
and it seems like a bug to me, unless there is now a different way that this is meant to be accomplished that I'm not seeing in the documentation. The problem is when providing aninclude
parameter for nested children of a resource (for example,?include=articles.author
), those "grandchild" objects are present in theincluded
but they have noattributes
.
I was able to recreate this with a simple setup:
Models
class Page(models.Model): passclass Author(models.Model): first_name = models.TextField() last_name = models.TextField()class Article(models.Model): page = models.ForeignKey(Page, related_name='articles', on_delete=models.CASCADE) author = models.ForeignKey(Author, related_name='articles', on_delete=models.CASCADE) title = models.TextField()
Serializers
class AuthorSerializer(serializers.ModelSerializer): class Meta: model = Author fields = ('id', 'first_name', 'last_name')class ArticleSerializer(serializers.ModelSerializer): author = AuthorSerializer(many=False, read_only=True) included_serializers = { 'author': AuthorSerializer, } class Meta: model = Article fields = ('id', 'title', 'author')class PageSerializer(serializers.ModelSerializer): articles = ArticleSerializer(many=True, read_only=True) included_serializers = { 'articles': ArticleSerializer, } class Meta: model = Page fields = ('id', 'articles')
The view is just a simpleModelViewSet
.
class PageViewSet(ModelViewSet): queryset = Page.objects.all() serializer_class = PageSerializer
The output of a query/page/?include=articles.author
is this:
{ "data": [ { "type": "Page", "id": "1", "attributes": {}, "relationships": { "articles": { "data": [ { "type": "Article", "id": "1" } ] } } } ], "included": [ { "type": "Article", "id": "1", "attributes": { "title": "Some Article" }, "relationships": { "author": { "data": { "type": "Author", "id": "1" } } } }, { "type": "Author", "id": "1", "attributes": { "first_name": null, "last_name": null } } ]}
Thefirst_name
andlast_name
fields for the included Author are present but have null values, when data exists in the database for this object. This issue seems to have been introduced indjango-rest-framework-json-api
v2.6.0, as the fields are returned correctly when using 2.5.0.