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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:03:52+00:00 2026-05-25T11:03:52+00:00

I’m a bit of a newbie in Python, so go easy on me. I’m

  • 0

I’m a bit of a newbie in Python, so go easy on me. I’m writing an AJAX handler in Django. Everything’s been pretty straight-forward on this until now. I’ve been banging my head against this bit for the better part of a day. I’d like to return a JSON string that contains a dict that contains a queryset:

#
# models.py
#

class Project(models.Model):
  unique_name  = models.CharField(max_length=32, unique=True)
  title        = models.CharField(max_length=255, blank=True)
  description  = models.TextField('project description', blank=True)
  project_date = models.DateField('Project completion date')
  published    = models.BooleanField()

class ProjectImage(models.Model):
  project      = models.ForeignKey('Project', related_name='images')
  image        = models.ImageField(upload_to=get_image_path)
  title        = models.CharField(max_length=255)
  sort_metric  = models.IntegerField()


#
# views.py
#
...
projects = Project.Project.objects.filter(published=True)
...
response_dict({
  'success' : True,
  'maxGroups' : 5, # the result of some analysis on the projects queryset
  'projects' : projects
});

# This one works if I remove 'projects' from the dict
# response = json.dumps( response_dict )

# This one works only on projects
# response = serializers.serialize( 'json', response_dict, relations=('images') )

return HttpResponse( response, mimetype='application/javascript' )

I’ve commented out the two serialization lines, because:

  1. The first one seems to only work with ‘simple’ dicts and since projects is included in my dict, it fails with [<Project: Project object>] is not JSON serializable
  2. The second one seems to only work with querysets/models and since the ‘outer’ part of my dict is non-model, it complains that 'str' object has no attribute '_meta'. Note that I am using the wadofstuff serializer with the understanding that it would resolve the OneToMany relationship as defined in my model. But even when I get this working by only serializing projects, I do not have any of my ProjectImages in the output.

QUESTION 1: What is the best way to serialize the whole response_dict? Surely, I’m not the first person to want to do this, right?

QUESTION 2: Why am I unable to get the ManyToOne relationship to work?

Many thanks for your help.

UPDATE: Just found this one: Django JSON Serialization with Mixed Django models and a Dictionary and it looked promising, but I get 'QuerySet' object has no attribute '_meta' =(

  • 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-25T11:03:53+00:00Added an answer on May 25, 2026 at 11:03 am

    You can’t serialize a python object like that. There is a section in the django documentation on what to do.

    https://docs.djangoproject.com/en/dev/topics/serialization/#id2

    Here is the key part to look at:

    json_serializer.serialize(queryset, ensure_ascii=False, stream=response)
    
    • 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&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I am writing an app with both english and french support. The app requests

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.