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.