Return as JSON string array instead of Key-Value array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Return as JSON string array instead of Key-Value array



I am writing a web service using Django and is trying to create an API that returns all distinct categories in a table which I have in MySQL database. The table schema is provided below:


+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| news_id | int(11) | YES | MUL | NULL | |
| news_category | varchar(50) | YES | | NULL | |
| publish_date | varchar(50) | YES | | NULL | |
| news_link | varchar(255) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+



The news_category field here is not unique. I want the API to list all distinct categories in the following JSON format:



"count": 22,
"next": null,
"previous": null,
"results": ["apple", "google", "microsoft", "apps", "photography", "virtual-reality", "business"]



Following is my models.py:


models.py


from django.db import models
...
class NewsInfo(models.Model):
news = models.ForeignKey(NewsContent, models.DO_NOTHING, blank=True, null=True)
news_category = models.CharField(max_length=50, blank=True, null=True)
publish_date = models.CharField(max_length=50, blank=True, null=True)
news_link = models.CharField(max_length=255)

def __str__(self):
return self.news.news_title

class Meta:
managed = False
db_table = 'news_info'



serializers.py:


serializers.py


from .models import NewsInfo
from rest_framework import serializers
...
class NewsInfoSerializer(serializers.ModelSerializer):
class Meta:
fields = ['news_category', 'news_link', 'news']
model = NewsInfo

class NewsCategorySerializer(serializers.ModelSerializer):
class Meta:
fields = ['news_category']
model = NewsInfo



views.py:


views.py


from rest_framework import viewsets
from .models import NewsInfo
from .serializers import NewsCategorySerializer
...
class CategoryViewSet(viewsets.ModelViewSet):
queryset = NewsInfo.objects.values('news_category').distinct()
serializer_class = NewsCategorySerializer



I am getting the response here, but as key value pairs like this:



"count": 22,
"next": null,
"previous": null,
"results": [
"news_category": "apple"
,
"news_category": "google"
,
"news_category": "microsoft"
,
"news_category": "apps"
,
"news_category": "photography"
,
"news_category": "virtual-reality"
,
"news_category": "business"
]



Whatever I try, I am not able to make the result as I want it. Please help.




1 Answer
1



Override to_representation() method of NewsCategorySerializer serializer as,


to_representation()


NewsCategorySerializer


class NewsCategorySerializer(serializers.ModelSerializer):
class Meta:
fields = ['news_category']
model = NewsInfo

def to_representation(self, instance):
return super().to_representation(instance)['news_category']





Yo!!! Again nailed it.. I am just starting to learn, will ask for help again!!! :D
– Harikrishnan
Aug 12 at 5:21





Just to know, what does this do anyway?
– Harikrishnan
Aug 12 at 5:22





Actually, this is not a Django Rest framework Way.
– JPG
Aug 12 at 5:23





the to_representation() is representing the data to API (to us). We override that
– JPG
Aug 12 at 5:24



to_representation()





Ok, got it, thanks!!! :)
– Harikrishnan
Aug 12 at 5:25






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS