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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:19:10+00:00 2026-05-24T11:19:10+00:00

I have a Django application that defines a one-to-many model relationship. The models look

  • 0

I have a Django application that defines a one-to-many model relationship. The models look like this:

from django.db import models

# Create your models here.
class StreamNetwork(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

class Stream(models.Model):
    name = models.CharField(max_length=50)
    custom_url = models.CharField(max_length=100, null=True)
    network = models.ForeignKey(StreamNetwork)
    score = models.SmallIntegerField()

    def __unicode__(self):
        return self.name

Later on I want to serialize all my Stream entries into json. I do it like this:

from django.core.serializers import serialize
string = serialize(format, Stream.objects.all())

The expected result would be a simple serialized json version of a row in the database. Instead the output looks like this:

[
    {
        "pk" : 1,
        "model" : "website.stream",
        "fields" : 
        {
            "network" : 2,
            "score" : 53,
            "name" : "DangerousDan1",
            "custom_url" : null
        }
    }, 
    ...

In addition I would like to get the name of the streamnetwork in the output instead of the id.

So is there a Django way I can customize how the serialize output should be formatted?

Thanks for any helpful replies

  • 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-05-24T11:19:11+00:00Added an answer on May 24, 2026 at 11:19 am

    Keep in mind that a QuerySet is more than just a simple database query, so the “expected result” should be that it contains additional information. One simple way to do what you want (and I’d say it’s a Django Way) is to define a model manager that returns only what you want. Something like:

    import simplejson
    
    class StreamManager(models.Manager):
        def all_serialized(self):
            qset = super(StreamManager, self).get_query_set()
            return simplejson.dumps(
                   [{ 'id':item.id, 
                      'name':item.name, 
                      'network':item.network.name, 
                      'score':item.score,
                      'custom_url':item.custom_url,}
                      for item in qset])
    

    then put objects = StreamManger() in your Stream class, and you can access the stream as Stream.objects.all_serialized()

    Another way to do it would be to subclass QuerySet so it returns only the information you want and then use the serialize() method on the queryset, but I’ve never gotten into that and assume it would require some knowledge of how QuerySets work under the hood (it might be slightly more efficient than my solution, but since the point is to serialize everything and you want to do a foreign key lookup on each entry to find the network name, I assume you not working with a huge amount of data).

    EDIT: regarding your comment on Rob’s solution, see the documentation for serialize() – you can define the fields you want to be serialized, but I don’t think it supports foreign key lookups, and you’ll still get it in the QuerySet format (pk, model, and fields).

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

Sidebar

Related Questions

I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have a many-to-many relationship in my django application where I use the add
I have a Django application that I would like to deploy to the desktop.
I have a django application that I'd like to add some rest interfaces to.
I have a Django application that reads data from a web API and puts
I have an application that uses Django 1.3 installed in python's site-packages. I want
I'm new to Django, but the application that I have in mind might end
I have a web application build in Django + Python that interact with web
I have a django application using multiple databases. Given an instance of a model,
I have inherited a large Django application and it features this idiom in the

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.