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 8476973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:19:35+00:00 2026-06-10T18:19:35+00:00

I have made an api that returns an object as json data. I Am

  • 0

I have made an api that returns an object as json data. I Am using the django-rest-framework and its serializer. Using the resources (ModelResource) I excluded some fields, like a property called ‘owner’. One of the fields is a foreignkey to itselve. I want to show this field in the api (so I use depth=2), but I want to exclude the same fields as I excluded in the object returning.
Is there a nice way to do this (I have tried several things without the wanted result).

This is my (simplified) code:
in models.py:

class MyObject(models.Model):
    name = models.CharField(max_length=256, blank=True)
    parent = models.ForeignKey('self',  blank=True,  null=True, default=None)
    and_some_otherfields = models.otherFields(....)
    owner = models.ForeignKey(User, null=True, blank=True, related_name='myobject_owner')

in resource.py:

class MyObjectResource(ModelResource):
    model = MyObject
    exclude = ('owner','and some other fields',)

and in the view used to return the object it returns this:

    data = Serializer(depth=2).serialize(my_object)
    return Response(status.HTTP_200_OK, data)

In the response it leaves out the exclude fields (as I wanted and expected).

but in the field parent, the parent myobject with all fields I want to hide.

I Am looking for a way to indicate that for this parent object, the serializer should use the same Resource, or add the secundary fields to the exclude list….

If I use depth =1 it only shows whether it has a parent ([]), or null if not, and i do need to know at least the parent’s ID.

  • 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-10T18:19:37+00:00Added an answer on June 10, 2026 at 6:19 pm

    Ah, i just found it:

    I need to add in the resource for all fields I want to show what resource….

    fields = (‘name’, (“parent”,”MyObjectResource”) , ‘and all the other fields you want to see as well…’)

    I found it here: google groups forum question

    You can skip the exlude, it ignores it, and just add the fields you want to show, you do not have to define them, unless you need to indicate what resource to use.

    So following is the final code of the resource.py part:

    class MyObjectResource(ModelResource):
        model = MyObject
        fields = ('name', ("parent","MyObjectResource"), 'and all the other fields you want to see as well...')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this 'web service' I made that when accessed: http://192.168.0.1/api/v1/search/?query=thequery returns a JSON
Using the Twitter Search API I have attempted to download JSON data from a
I have made my own map using the google maps api. It is a
i am using your api and i have made a success call to upload
I have made a code that read data from flash Nand (without filesystem). fd
I have made a website using( Asp.net, c# ) and its content in English
I try to query Sugar through its REST API using Java for entries in
I have a REST API using Spring. I've created an Interceptor: @Component public class
I am currently building a website that uses data from a JSON based REST
I have made a jQuery toggle for a menu that I had in mind.

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.