Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:49:06+00:00 2026-06-14T06:49:06+00:00

I have model that looks like this: class Category(models.Model): parentCategory = models.ForeignKey(‘self’, blank=True, null=True,

  • 0

I have model that looks like this:

class Category(models.Model):
    parentCategory = models.ForeignKey('self', blank=True, null=True, related_name='subcategories')
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=500)

I managed to get flat json representation of all categories with serializer:

class CategorySerializer(serializers.HyperlinkedModelSerializer):
    parentCategory = serializers.PrimaryKeyRelatedField()
    subcategories = serializers.ManyRelatedField()

    class Meta:
        model = Category
        fields = ('parentCategory', 'name', 'description', 'subcategories')

Now what I want to do is for subcategories list to have inline json representation of subcategories instead of their ids. How would I do that with django-rest-framework? I tried to find it in documentation, but it seems incomplete.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:49:07+00:00Added an answer on June 14, 2026 at 6:49 am

    Instead of using ManyRelatedField, use a nested serializer as your field:

    class SubCategorySerializer(serializers.ModelSerializer):
        class Meta:
            model = Category
            fields = ('name', 'description')
    
    class CategorySerializer(serializers.ModelSerializer):
        parentCategory = serializers.PrimaryKeyRelatedField()
        subcategories = serializers.SubCategorySerializer()
    
        class Meta:
            model = Category
            fields = ('parentCategory', 'name', 'description', 'subcategories')
    

    If you want to deal with arbitrarily nested fields you should take a look at the customising the default fields part of the docs. You can’t currently directly declare a serializer as a field on itself, but you can use these methods to override what fields are used by default.

    class CategorySerializer(serializers.ModelSerializer):
        parentCategory = serializers.PrimaryKeyRelatedField()
    
        class Meta:
            model = Category
            fields = ('parentCategory', 'name', 'description', 'subcategories')
    
            def get_related_field(self, model_field):
                # Handles initializing the `subcategories` field
                return CategorySerializer()
    

    Actually, as you’ve noted the above isn’t quite right.
    This is a bit of a hack, but you might try adding the field in after the serializer is already declared.

    class CategorySerializer(serializers.ModelSerializer):
        parentCategory = serializers.PrimaryKeyRelatedField()
    
        class Meta:
            model = Category
            fields = ('parentCategory', 'name', 'description', 'subcategories')
    
    CategorySerializer.base_fields['subcategories'] = CategorySerializer()
    

    A mechanism of declaring recursive relationships is something that needs to be added.


    Edit: Note that there is now a third-party package available that specifically deals with this kind of use-case. See djangorestframework-recursive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have a model that looks like this: class Category(models.Model): name = models.CharField(max_length=60) class
I have a model that looks like this class RSVP (models.Model): def __unicode__(self): return
I have Django model that looks like this: class Categories(models.Model): Model for storing the
In my application I have a model that looks like this: class Talk(models.Model): title
I have a model that looks like this: class ProjectImage(models.Model): big_thumb = ThumbnailField(upload_to='profiles', size=(500,
I have a model that looks something like this class Post(models.Model): id = models.IntegerField(unique=True,
I have a model that looks like this: class Base(models.Model): pass class Mixin1(Base): active
I have a model that looks like this: class Movie(models.Model): title = models.CharField(max_length=200) slug
I have a Model that looks something like this: class Business(models.Model): name = models.CharField('business

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.