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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:21:05+00:00 2026-06-16T10:21:05+00:00

I want to put a Django model in de response for my Ajax request.

  • 0

I want to put a Django model in de response for my Ajax request. Currently I have this in my views.py:

def get_account(request, account_id):

    try:
        account = Account.objects.get(pk=account_id)
        success = True
        error_message = None
    except Account.DoesNotExist:
        success = False
        error_message = 'This account does not exist'

    results = {
        'success': success,
        'error_message': error_message,
    }

    return HttpResponse(
        json.dumps(results),
        mimetype='application/json')

I would like to add the account model to the results dict. account.__dict__ won’t do because it will have references to other objects in it.

I found de django serialize function, which serializes it exactly how I want it, only it makes a Json string directly, so I’ll end up with a Json string inside a Json object (bad for bandwith if you have large models, because the Json string get’s escaped all over). So then I would need to json_decode it another time in Javascript.

Also, the django serialize function only accepts lists of objects, so I would have to make a list with only one object, and when I unserialize it take the first value of the list (which isn’t a super big problem, but it adds up to the pile).

It would be great if you could serialize a model to just a python dict. Then you can do anything with it as you like.

Anyone ever got the same problem? How did you solve it?

  • 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-16T10:21:06+00:00Added an answer on June 16, 2026 at 10:21 am

    With the help of Daniel’s answer I was able to do it:

    def get_account_data(account):
        fields = serializers.serialize('python', [account, ])[0]['fields']
        return {field: str(fields[field]) for field in fields}
    

    Because I’m sending it over Ajax, I needed everything to be a string. If you serialize using the ‘python’ method, some things like a datetime field will have a function in it (like datetime.date(2012, 12, 23)). This function makes a dict with only string values.

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

Sidebar

Related Questions

I am using a file upload in my Django model like this : def
I've projected an Intranet Ajax application and I want put it in a full
hey, hi i want put limit on object creation means a class can have
I want to put two side by side on another (I have three )
In Django, I have a model, and I will have a dictionary having the
I am using the standard User model (django.contrib.auth) which comes with Django. I have
I'm currently working on my first Django project and have hit a roadblock in
I'm gonna try and simplify this as much as possible. Lets say i have
I need to re-order a Django queryset, because I want to put None values
This is a follow-up question on Delete field from standard Django model . In

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.