Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango custom field with multiple inheritance
    text
    copied!<p>I have two custom Django fields, a <code>JSONField</code> and a <code>CompressedField</code>, both of which work well. I would like to also have a <code>CompressedJSONField</code>, and I was rather hoping I could do this:</p> <pre><code>class CompressedJSONField(JSONField, CompressedField): pass </code></pre> <p>but on import I get:</p> <pre><code>RuntimeError: maximum recursion depth exceeded while calling a Python object </code></pre> <p>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?</p> <p><strong>edit:</strong></p> <p>Just to be clear, I don't <em>think</em> this has anything to do with the specifics of my code, as the following code has exactly the same problem:</p> <pre><code>class CustomField(models.TextField, models.CharField): pass </code></pre> <p><strong>edit 2:</strong></p> <p>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:</p> <h2><code>customfields.py</code></h2> <pre><code>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 </code></pre> <h2><code>models.py</code></h2> <pre><code>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 </code></pre> <p>as soon as I add the <code>compressed_json_field = CompressedJSONField()</code> line I get errors when initializing Django.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload