我有两个自定义 Django 字段, aJSONField
和 a CompressedField
,它们都运行良好。我也想有一个CompressedJSONField
,我宁愿希望我能做到这一点:
class CompressedJSONField(JSONField, CompressedField):
pass
但在导入时我得到:
RuntimeError: maximum recursion depth exceeded while calling a Python object
我可以找到有关在 Django 中使用具有多重继承的模型的信息,但没有关于对字段执行相同操作的信息。这应该可能吗?还是我应该在这个阶段放弃?
编辑:
为了清楚起见,我认为这与我的代码的细节没有任何关系,因为以下代码具有完全相同的问题:
class CustomField(models.TextField, models.CharField):
pass
编辑2:
我目前正在使用 Python 2.6.6 和 Django 1.3。这是我剥离的右下测试示例的完整代码:
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
一旦我添加该compressed_json_field = CompressedJSONField()
行,初始化 Django 时就会出错。