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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:28:07+00:00 2026-06-13T06:28:07+00:00

I am trying to serialize a list of model object defined as: class AnalysisInput(models.Model):

  • 0

I am trying to serialize a list of model object defined as:

class AnalysisInput(models.Model):
    input_user = models.CharField(max_length=45)
    input_title = models.CharField(max_length=45)
    input_date = models.DateTimeField()
    input_link = models.CharField(max_length=100)

I wrote a custom serializer (encoder) for json.dumps():

class AnalysisInputEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, AnalysisInput):
            return { "input_id" : obj.id,
                    "input_user" : obj.input_user,
                    "input_title" : obj.input_title,
                    "input_date" : obj.input_date.isoformat(),
                    "input_link" : obj.input_link }
        return json.JSONEncoder.default(self, obj)

When I serialize only one object, I am able to do it. When I try to serialize a list of object I get

[ objects..] is not JSON serializable

I searched but I didn’t find where to work on.. I was thinking about writing a custom serializer also for list of model object.

  • 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-13T06:28:08+00:00Added an answer on June 13, 2026 at 6:28 am

    A custom encoder is not called recursively. You are actually better off not using a custom encoder, and instead convert your objects to simple python types before serializing.

    You could add a as_json or similarly named method to your model and calling that every time you need a JSON result:

    class AnalysisInput(models.Model):
        input_user = models.CharField(max_length=45)
        input_title = models.CharField(max_length=45)
        input_date = models.DateTimeField()
        input_link = models.CharField(max_length=100)
    
        def as_json(self):
            return dict(
                input_id=self.id, input_user=self.input_user,
                input_title=self.input_title, 
                input_date=self.input_date.isoformat(),
                input_link=self.input_link)
    

    Then in your view:

    # one result
    return HttpResponse(json.dumps(result.as_json()), content_type="application/json")
    
    # a list of results
    results = [ob.as_json() for ob in resultset]
    return HttpResponse(json.dumps(results), content_type="application/json")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to Serialize a Class object and store the xml in a
I'm trying to serialize and deserialize an array list with a object inside: HairBirt
I am getting the following error message when trying to serialize List<Tuple<string, Type, object>>
When trying to serialize a type (a generic List<T> which T is a class
I have been trying to serialize a list that contains arrays and lists. I
I am trying to serialize a large (~10**6 rows, each with ~20 values) list,
I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like
I'm trying to serialize an object to XML that has a number of properties,
I have been trying to serialize a pretty big object. This object uses dictionaries
I've been trying to serialize an XML (jQuery object) to string to POST it

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.