JSON validation from DRF

86 views
Skip to first unread message

: Anthony: Crawford.

unread,
Nov 1, 2023, 6:10:41 PM11/1/23
to Django REST framework
Hi,
I am trying to create an API that will provide JSON data that is based on Roku JSON specification (https://developer.roku.com/en-ca/docs/specs/direct-publisher-feed-specs/json-dp-spec.md).
The specification requires that the JSON data is a single object { }, not a list array [ ].
So far I am not able to setup the APIView to render JSON data as an object { }.

The ListAPIView always serializes the JSON to an array and not a single object. However, RetrieveAPIView serializes the JSON as a single object.

A sample of the Roku JSON feed:

I am fairly new to this, and learning as I go. 

code:

```
class RokuContentFeedAPI(APIView):
def get(self, request, format=None):
feed = RokuContentFeed.objects.all().filter(is_public=True)
serializer = RokuContentFeedSerializer(feed, many=True)
return Response(serializer.data) 
```

```
class RokuContentFeedSerializer(serializers.ModelSerializer):
providerName = serializers.CharField(source='provider_name')
language = serializers.StringRelatedField()
rating = RatingSerializerList()
lastUpdated = serializers.DateTimeField(source='last_updated')
movies = MovieSerializerList(many=True)
liveFeeds = LiveFeedSerializerList(many=True, source='live_feeds')
series = SeriesSerializerList(many=True)
shortFormVideos = ShortFormVideoSerializerList(many=True, source='short_form_videos')
tvSpecials = TVSpecialSerializerList(many=True, source='tv_specials')
categories = CategorySerializerList(many=True)
playlists = PlaylistSerializerList(many=True)
class Meta:
model = RokuContentFeed
#read_only_fields = ['is_public']
fields = ['providerName', 'language', 'rating', 'lastUpdated', 'movies', \
'liveFeeds', 'series', 'shortFormVideos', 'tvSpecials', 'categories', 'playlists']
```
Thank for any suggestions
AC

data.json

Leonardo Salvadori

unread,
Nov 3, 2023, 7:42:38 AM11/3/23
to django-res...@googlegroups.com
hi Anthony, as i know, the "many" parameter in your RokuContentFeedSerializer method makes your data an array [ ]. 

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/56f7c360-4ac4-4229-b0bd-effbd68d9b37n%40googlegroups.com.

: Anthony: Crawford.

unread,
Nov 18, 2023, 7:37:28 PM11/18/23
to Django REST framework
Thank you, yes, many=True will turn any field a list. However my issue is that this response data is the main object. It is returning a list of objects [ { } ] instead of one object { }. I changed my view to contain this code for now, but its defintely not a final solution. This code will return a single { } object.

```
class RokuContentFeedListAPI(APIView):
renderer_classes = [JSONRenderer]
pagination_class = None
def get(self, request, format=None):
feeds = RokuContentFeed.objects.first()
serializer = RokuContentFeedSerializerList(feeds)
return Response(serializer.data)
```

Mehmet Gürol Çay

unread,
Nov 20, 2023, 3:22:36 AM11/20/23
to Django REST framework
As I see in your code, I think you misunderstood the RetrieveAPIView. You cannot get a single object while giving multiple objects. If you create a RetrieveAPIView, you need to know which object is selected. I mean, you need to pass the ID of RokuContentFeed to your get method of RokuContentFeedRetrieveAPI.

code:

```
class RokuContentFeedListAPI(APIView):
   def get(self, request, format=None): 
       feeds = RokuContentFeed.objects.all().filter(is_public=True)
       serializer = RokuContentFeedSerializer(feeds, many=True)
       return Response(serializer.data) 
```

class RokuContentFeedRetrieveApi(APIView):
    def get(self, request, pk, format=None):
           feed = RokuContentFeed.objects.get(pk=pk)
           serializer = RokuContentFeedSerializer(feed)
           return Response(serializer.data) 
```
class RokuContentFeedSerializer(serializers.ModelSerializer):
   providerName = serializers.CharField(source='provider_name')
   language = serializers.StringRelatedField()
   rating = RatingSerializerList()
   lastUpdated = serializers.DateTimeField(source='last_updated')
   movies = MovieSerializerList(many=True)
   liveFeeds = LiveFeedSerializerList(many=True, source='live_feeds')
   series = SeriesSerializerList(many=True)
   shortFormVideos = ShortFormVideoSerializerList(many=True, source='short_form_videos')
   tvSpecials = TVSpecialSerializerList(many=True, source='tv_specials')
   categories = CategorySerializerList(many=True)
   playlists = PlaylistSerializerList(many=True)
   class Meta:
       model = RokuContentFeed
       #read_only_fields = ['is_public']
       fields = ['providerName', 'language', 'rating', 'lastUpdated', 'movies', \
           'liveFeeds', 'series', 'shortFormVideos', 'tvSpecials', 'categories', 'playlists']
```


19 Kasım 2023 Pazar tarihinde saat 03:37:28 UTC+3 itibarıyla : Anthony: Crawford. şunları yazdı:

: Anthony: Crawford.

unread,
Nov 20, 2023, 8:00:21 PM11/20/23
to Django REST framework
Thank you for your help. This got me going. Cheers
Reply all
Reply to author
Forward
0 new messages