I have two models
- Influencer
class Influencer(models.Model):
full_name = models.CharField('Full Name',max_length=255)
username = models.CharField('Username',max_length=255,unique=True)
photo = models.URLField(blank=True,max_length = 500)
email_id = models.EmailField('Email Id',blank=True,max_length=500)
categories = models.ManyToManyField(Category,blank=True,max_length=400)
and 2. Categories
class Category(models.Model):
name = models.CharField(max_length=400)
def __str__(self):
return self.name
Influencer has a many to many field categories.
My views functions to display all the influencers is:
def index(request):
influencers = Influencer.objects.all().order_by('followers')
paginator = Paginator(influencers,16)
page = request.GET.get('page')
paged_listings = paginator.get_page(page)
user_list = UserList.objects.all().filter(user_id = request.user.id)
queryset = list(chain(paged_listings,user_list))
ser_query = serializers.serialize('json', queryset)
return HttpResponse(ser_query,content_type='application/json')
The HttpResponse contains category id's instead of category names, something like this:

where categories is an array which contains category id's. I want to display the name of categories instead of their id's.
I think this can be achived using Django Rest Framework nested serializer, but at this moment I am not using DRF.