Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Django's JSON serialization is based on simplejson, which you can use directly and extend at will to handle whatever kind of objects. So you mostly have two options here: either manually build a list of dicts with relevant data and pass it to simplejson.dumps() (which by default support strings, lists, dicts and numerics), or write your own json encoder that knows how to serialize your specific dataset. FWIW, here's a (not well tested but worked so far) Django model aware json encoder:</p> <pre><code>from django.utils import simplejson from django.utils import datetime_safe from django.utils.functional import Promise from django.utils.translation import force_unicode from django.utils.encoding import smart_unicode from django.core.serializers.json import DjangoJSONEncoder class ModelJSONEncoder(DjangoJSONEncoder): """ (simplejson) DjangoJSONEncoder subclass that knows how to encode fields. (adated from django.serializers, which, strangely, didn't factor out this part of the algorithm) """ def handle_field(self, obj, field): return smart_unicode(getattr(obj, field.name), strings_only=True) def handle_fk_field(self, obj, field): related = getattr(obj, field.name) if related is not None: if field.rel.field_name == related._meta.pk.name: # Related to remote object via primary key related = related._get_pk_val() else: # Related to remote object via other field related = getattr(related, field.rel.field_name) return smart_unicode(related, strings_only=True) def handle_m2m_field(self, obj, field): if field.creates_table: return [ smart_unicode(related._get_pk_val(), strings_only=True) for related in getattr(obj, field.name).iterator() ] def handle_model(self, obj): dic = {} for field in obj._meta.local_fields: if field.serialize: if field.rel is None: dic[field.name] = self.handle_field(obj, field) else: dic[field.name] = self.handle_fk_field(obj, field) for field in obj._meta.many_to_many: if field.serialize: dic[field.name] = self.handle_m2m_field(obj, field) return dic def default(self, obj): if isinstance(o, Promise): return force_unicode(o) if isinstance(obj, Model): return self.handle_model(obj) return super(ModelJSONEncoder, self).default(obj) </code></pre> <p>HTH</p>
    singulars
    1. This table or related slice is empty.
    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. 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