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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:08:48+00:00 2026-06-03T19:08:48+00:00

Serialize django model with foreign key models Serializing Foreign Key objects in Django get

  • 0
  • Serialize django model with foreign key models
  • Serializing Foreign Key objects in Django
  • get foreign key objects in a single query – Django

There are couple of question asking for the same thing already. But they are from 2010, and it didn’t help me so much. So I figure it maybe been some update to this front since 2010?

On google I found this link, which explain usage of natural keys. However my problem concerns of getting foreign objects from django.contrib.auth.models.User so it doesn’t help.

My problem is as following. I want to serialize the QuerySet so I get the foreign key objects also, because I want to pass it as JSON to the client. The serializer from django.core doesn’t do that. So in my case to simply the problem I had added another field to the model to contain the value I need from the foreign object. But it however introduce redundant data.

My example model it contains the username which I would like if possible remove, and instead get it by the foreign key.

    user = models.ForeignKey(User)
    username = models.CharField(max_length=100, null=False)
  • 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-03T19:08:53+00:00Added an answer on June 3, 2026 at 7:08 pm

    One potential way around this is to construct your own dictionary object based on the returns of a queryset. You’d do something like this:

    queryset = Model.objects.all()
    list = [] #create list
    for row in queryset: #populate list
        list.append({'title':row.title, 'body': row.body, 'name': row.user.username})
    recipe_list_json = json.dumps(list) #dump list as JSON
    return HttpResponse(recipe_list_json, 'application/javascript')
    

    You need to import json for this to work.

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

Sidebar

Related Questions

How to serialize Django model in json format if I want to include foreign
Is there a way I can serialize a FloatField model instance in django? I
I'm trying to figure out a way to serialize some Django model object to
How to serialize Multiple Objects of the same Class into a single file and
I can't seem to find a good way to serialize both Django Models and
I plan to serialize a Django model to XML when it's saved or updated.
I have a Django application that defines a one-to-many model relationship. The models look
Django has a built in serialization functionality which allows you to serialize any query
What's the common practice to represent postal addresses in Django models? Is there a
When serializing a django model object, the format is like this: { pk:1, model:book,

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.