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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:00:07+00:00 2026-05-21T07:00:07+00:00

I have two custom Django fields, a JSONField and a CompressedField , both of

  • 0

I have two custom Django fields, a JSONField and a CompressedField, both of which work well. I would like to also have a CompressedJSONField, and I was rather hoping I could do this:

class CompressedJSONField(JSONField, CompressedField):
    pass

but on import I get:

RuntimeError: maximum recursion depth exceeded while calling a Python object

I can find information about using models with multiple inheritance in Django, but nothing about doing the same with fields. Should this be possible? Or should I just give up at this stage?

edit:

Just to be clear, I don’t think this has anything to do with the specifics of my code, as the following code has exactly the same problem:

class CustomField(models.TextField, models.CharField):
    pass

edit 2:

I’m using Python 2.6.6 and Django 1.3 at present. Here is the full code of my stripped-right-down test example:

customfields.py

from django.db import models


class CompressedField(models.TextField):
    """ Standard TextField with automatic compression/decompression. """

    __metaclass__ = models.SubfieldBase
    description = 'Field which compresses stored data.'

    def to_python(self, value):
        return value

    def get_db_prep_value(self, value, **kwargs):
        return super(CompressedField, self)\
                        .get_db_prep_value(value, prepared=True)


class JSONField(models.TextField):
    """ JSONField with automatic serialization/deserialization. """

    __metaclass__ = models.SubfieldBase
    description = 'Field which stores a JSON object'

    def to_python(self, value):
        return value

    def get_db_prep_save(self, value, **kwargs):
        return super(JSONField, self).get_db_prep_save(value, **kwargs)


class CompressedJSONField(JSONField, CompressedField):
    pass

models.py

from django.db import models
from customfields import CompressedField, JSONField, CompressedJSONField

class TestModel(models.Model):

    name = models.CharField(max_length=150)
    compressed_field = CompressedField()
    json_field = JSONField()
    compressed_json_field = CompressedJSONField()

    def __unicode__(self):
        return self.name

as soon as I add the compressed_json_field = CompressedJSONField() line I get errors when initializing Django.

  • 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-21T07:00:08+00:00Added an answer on May 21, 2026 at 7:00 am

    after doing a few quick tests i found that if you remove the metaclass from the JSON and compressed fields and put it in the compressedJSON field it compiles. if you then need the JSON or Compressed fields then subclass them and jusst add the __metaclass__ = models.SubfieldBase

    i have to admit that i didn’t do any heavy testing with this:

    from django.db import models                                                       
    
    
    class CompressedField(models.TextField):                                           
        """ Standard TextField with automatic compression/decompression. """           
    
        description = 'Field which compresses stored data.'                            
    
        def to_python(self, value):                                                    
            return value                                                               
    
        def get_db_prep_value(self, value, **kwargs):                                  
            return super(CompressedField, self).get_db_prep_value(value, prepared=True)
    
    
    class JSONField(models.TextField):                                                 
        """ JSONField with automatic serialization/deserialization. """                
    
        description = 'Field which stores a JSON object'                               
    
        def to_python(self, value):                                                    
            return value 
    
        def get_db_prep_save(self, value, **kwargs):                                   
            return super(JSONField, self).get_db_prep_save(value, **kwargs)            
    
    
    class CompressedJSONField(JSONField, CompressedField):                             
        __metaclass__ = models.SubfieldBase                                            
    
    class TestModel(models.Model):                                                     
    
        name = models.CharField(max_length=150)                                        
        #compressed_field = CompressedField()                                          
        #json_field = JSONField()                                                      
        compressed_json_field = CompressedJSONField()                                  
    
        def __unicode__(self):                                                         
            return self.name
    

    if you then want to uses the JSON and Commpressed fields separately i assume this idea will work:

    class JSONFieldSubClass(JSONField):
        __metaclass__ = models.SubfieldBase
    

    Honestly … I don’t really understand any of this.

    EDIT base method hack

    class CompressedJSONField(JSONField, CompressedField):
        __metaclass__ = models.SubfieldBase
    
        def to_python(self, value):
            value = JSONField.to_python(self, value)
            value = CompressedField.to_python(self, value)
            return value
    

    the other way is to make the to_python() on the classes have unique names and call them in your inherited classes to_python() methods

    or maybe check out this answer

    EDIT
    after some reading if you implement a call to super(class, self).method(args) in the first base to_python() then it will call the second base. If you use super consistently then you shouldn’t have any problems. http://docs.python.org/library/functions.html#super is worth checking out and http://www.artima.com/weblogs/viewpost.jsp?thread=237121

    class base1(object):                                                               
        def name(self, value):                                                         
            print "base1", value                                                       
            super(base1, self).name(value)                                             
    
        def to_python(self, value):                                                    
            value = value + " base 1 "                                                 
            if(hasattr(super(base1, self), "to_python")):                              
                value = super(base1, self).to_python(value)                            
            return value                                                               
    
    class base2(object):                                                               
        def name(self, value):                                                         
            print "base2", value                                                       
    
        def to_python(self, value):                                                    
            value = value + " base 2 "                                                 
            if(hasattr(super(base2, self), "to_python")):                              
                value = super(base2, self).to_python(value)                            
            return value                                                               
    
    class superClass(base1, base2):                                                    
        def name(self, value):                                                         
            super(superClass, self).name(value)                                        
            print "super Class", value    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DUPLICATE: Using a Django custom model method property in order_by() I have two models;
I have a model where I would like to make a custom admin change_form
So I have two custom complex types like this (oversimplified for this example): public
I have two custom controls that are analogous to a node and the control
I have two lists of custom objects and want to update a field for
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two Django model classes that are structured similar to the following: class
If I have two models in Django: class Blog(models.Model): author = models.CharField() class Post(models.Model):
I have two custom objects, Phrase Book and Phrase, the Phrase Book has an
I have two Spark Lists with custom Item Renderers. I'm working on an application

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.