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

  • Home
  • SEARCH
  • 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 8751655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:05:26+00:00 2026-06-13T13:05:26+00:00

I’ve just started using Tastypie and am trying to work out how to format

  • 0

I’ve just started using Tastypie and am trying to work out how to format the output as I would like (I’m only interested in GET methods).
I have a quiz object which may have multiple questions, each question may be in multiple quizzes (hence the many to many, rather than one to many) – but when someone requests the quiz object via the API, I only want to return a (json) array of the questions, I don’t want/need to display the intermediate relationship table data.

Some code:

Models.py:

class Question(models.Model):
    owner = models.ForeignKey(User)
    created_date = models.DateTimeField('date created',default=datetime.now)
    lastupdated_date = models.DateTimeField('date updated',default=datetime.now)
    title = models.CharField(max_length=500)   
    def __unicode__(self):
        return self.title

class Quiz(models.Model):
    owner = models.ForeignKey(User)
    created_date = models.DateTimeField('date created',default=datetime.now)
    lastupdated_date = models.DateTimeField('date updated',default=datetime.now)
    title = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    props = models.TextField(blank=True)
    questions = models.ManyToManyField(Question, through='QuizQuestion')

    def __unicode__(self):
        return self.title


class QuizQuestion(models.Model):
    quiz = models.ForeignKey(Quiz)
    question = models.ForeignKey(Question)
    order = models.IntegerField(default=1)

and resources.py:

class QuizResource(ModelResource):
    q = fields.ToManyField('mquiz.api.resources.QuizQuestionResource', 'quizquestion_set', related_name='quiz', full=True)
    class Meta:
        queryset = Quiz.objects.all()
        allowed_methods = ['get']
        fields = ['title', 'id']
        resource_name = 'quiz'
        include_resource_uri = False

class QuizQuestionResource(ModelResource):
    question = fields.ToOneField('mquiz.api.resources.QuestionResource', 'question', full=True)
    class Meta:
        queryset = QuizQuestion.objects.all()
        allowed_methods = ['get']
        include_resource_uri = False

class QuestionResource(ModelResource): 
    class Meta:
        queryset = Question.objects.all()
        allowed_methods = ['get']
        fields = ['title']
        resource_name = 'question'
        include_resource_uri = False

This functions well enough, but doesn’t quite give the output I’d like. It gives me the output:

{
  "id": "1",
  "q": [
    {
      "id": "1",
      "order": 1,
      "question": {
        "title": "What is the capital of Latvia?"
      }
    },
    {
      "id": "2",
      "order": 2,
      "question": {
        "title": "What is the capital of Ethiopia?"
      }
    }
  ],
  "title": "Capitals"
}

However, what I would really like is the output in this format, since I don’t need to have all the intermediate table id/order fields showing:

{
  "id": "1",
  "q": [
    {
        "title": "What is the capital of Latvia?"
    },
    {
        "title": "What is the capital of Ethiopia?"
    }
  ],
  "title": "Capitals"
}

Is there a way to achieve this? Any help/pointers much appreciated.

UPDATE:
Using a custom serialiser like this seems to work:

class QuizJSONSerializer(Serializer):
    json_indent = 2

    def to_json(self, data, options=None):
        options = options or {}
        data = self.to_simple(data, options)
        for question in data['q']:
            del question['id']
            del question['order']
            for qkey, qvalue in question['question'].items():
                question[qkey] = qvalue
            del question['question']
        return simplejson.dumps(data, cls=json.DjangoJSONEncoder,
                sort_keys=True, ensure_ascii=False, indent=self.json_indent)

Sure there may be more generic way to code this – but works for now.

  • 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-13T13:05:27+00:00Added an answer on June 13, 2026 at 1:05 pm

    Overriding the serialize function of the resource object with your own code would be a good approach, but refactoring the data bundle within the dehydrate method would be a quick fix

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I would like to count the length of a string with PHP. The string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.