Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>after doing a few quick tests i found that if you remove the <strong>metaclass</strong> 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 <code>__metaclass__ = models.SubfieldBase</code></p> <p>i have to admit that i didn't do any heavy testing with this:</p> <pre><code>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 </code></pre> <p>if you then want to uses the JSON and Commpressed fields separately i assume this idea will work:</p> <pre><code>class JSONFieldSubClass(JSONField): __metaclass__ = models.SubfieldBase </code></pre> <p>Honestly ... I don't really understand any of this.</p> <p><strong>EDIT base method hack</strong></p> <pre><code>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 </code></pre> <p>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</p> <p>or maybe check out this <a href="https://stackoverflow.com/questions/2611892/get-python-class-parents/2611897#2611897">answer</a></p> <p><strong>EDIT</strong> after some reading if you implement a call to <code>super(class, self).method(args)</code> 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. <a href="http://docs.python.org/library/functions.html#super" rel="nofollow noreferrer">http://docs.python.org/library/functions.html#super</a> is worth checking out and <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=237121" rel="nofollow noreferrer">http://www.artima.com/weblogs/viewpost.jsp?thread=237121</a> </p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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