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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:45:45+00:00 2026-05-12T15:45:45+00:00

I have a simple view that I’m using to experiment with AJAX. def get_shifts_for_day(request,year,month,day):

  • 0

I have a simple view that I’m using to experiment with AJAX.

def get_shifts_for_day(request,year,month,day):

    data= dict()
    data['d'] =year
    data['e'] = month
    data['x'] = User.objects.all()[2]

    return HttpResponse(simplejson.dumps(data), mimetype='application/javascript')

This returns the following:

TypeError at /sched/shifts/2009/11/9/

<User: someguy> is not JSON serializable

If I take out the data[‘x’] line so that I’m not referencing any models it works and returns this:

{"e": "11", "d": "2009"}

Why can’t simplejson parse my one of the default django models? I get the same behavior with any model I use.

  • 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-12T15:45:46+00:00Added an answer on May 12, 2026 at 3:45 pm

    You just need to add, in your .dumps call, a default=encode_myway argument to let simplejson know what to do when you pass it data whose types it does not know — the answer to your “why” question is of course that you haven’t told poor simplejson what to DO with one of your models’ instances.

    And of course you need to write encode_myway to provide JSON-encodable data, e.g.:

    def encode_myway(obj):
      if isinstance(obj, User):
        return [obj.username,
                obj.firstname,
                obj.lastname,
                obj.email]
        # and/or whatever else
      elif isinstance(obj, OtherModel):
        return [] # whatever
      elif ...
      else:
        raise TypeError(repr(obj) + " is not JSON serializable")
    

    Basically, JSON knows about VERY elementary data types (strings, ints and floats, grouped into dicts and lists) — it’s YOUR responsibility as an application programmer to match everything else into/from such elementary data types, and in simplejson that’s typically done through a function passed to default= at dump or dumps time.

    Alternatively, you can use the json serializer that’s part of Django, see the docs.

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

Sidebar

Related Questions

I have a simple query (in a mySQL view) that php is using to
I have a simple view that I want to respond to both ajax and
I have a simple Django view that just returns URL parameters, but if I
I have a simple custom view that is connected via outlet to a NIB.
I have an iPhone app that shows a simple view (View 1) that has
I have a grid view that has two simple columns, a report number and
I have a partial view (UserControl) that implements a simple pager in my Asp.Net
I have an extremely simple page that I'm trying to view in the Facebook
I have a very simple Mac Cocoa app that just has a Web View
I am using the split view template to create a simple split view that

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.