Login

Tag "serialize"

Snippet List

Dump a model instance and related objects as a Python data structure

This utility makes a text dump of a model instance, including objects related by a forward or reverse foreign key. The result is a hierarchical data structure where * each instance is represented as a list of fields, * each field as a (<name>, <value>) tuple, * each <value> as a primitive type, a related object (as a list of fields), or a list of related objects. See the docstring for examples. We used this to make text dumps of parts of the database before and after running a batch job. The format was more useful than stock `dumpdata` output since all related data is included with each object. These dumps lend themselves particularly well for comparison with a visual diff tool like [Meld](http://meldmerge.org/).

  • serialize
  • dump
  • model
  • instance
Read More

Deep json serialization

Custom serialization, the poor try to make something like [django full serializers](http://code.google.com/p/wadofstuff/wiki/DjangoFullSerializers) Usage: you need two files, goodjson.py and goodpython.py, for example, in the root of application named "core". Then, add two lines into your settings.py: SERIALIZATION_MODULES = {'goodjson' : 'core.goodjson', 'goodpython': 'core.goodpython'} This thing does only serialization, not deserialization. You were warned.

  • serialize
  • json
  • serialization
  • deep
Read More

Export Related as JSON Admin Action

[The Django Admin Action documentation leads you through exporting a queryset to JSON](http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#actions-that-provide-intermediate-pages). However exporting from a single model rarely tells the whole story. Using the CollectObjects class, `export_related_as_json` gathers all instances related by foreign keys to what is being exported and exports them as well in a serialization bonanza. Use it to export Users and you'll get their Profile objects as well! **Usage** # admin.py from django.contrib import admin admin.site.add_action(export_related_as_json)

  • serialize
  • admin
  • json
  • export
  • admin-action
Read More

Improved YAML serializer for large databases

I needed the ability to serialize and deserialize my database, which contains millions of objects. The existing XML serializer encountered spurious parse errors; the JSON serializer failed to handle UTF-8 even when it was asked to; and both the JSON and YAML serializers tried to keep all the representations in memory simultaneously. This custom serializer is the only one that has done the job. It uses YAML's "stream of documents" model so that it can successfully serialize and deserialize large databases.

  • serialize
  • database
  • yaml
Read More

Command to make fixtures.

"Make fixture" command. Highly useful for making test fixtures. Use it to pick only few items from your data to serialize, restricted by primary keys. By default command also serializes foreign keys and m2m relations. You can turn off related items serialization with `--skip-related` option. How to use: python manage.py makefixture will display what models are installed python manage.py makefixture User[:3] or python manage.py makefixture auth.User[:3] or python manage.py makefixture django.contrib.auth.User[:3] will serialize users with ids 1 and 2, with assigned groups, permissions and content types. python manage.py makefixture YourModel[3] YourModel[6:10] will serialize YourModel with key 3 and keys 6 to 9 inclusively. Of course, you can serialize whole tables, and also different tables at once, and use options of dumpdata: python manage.py makefixture --format=xml --indent=4 YourModel[3] AnotherModel auth.User[:5] auth.Group

  • serialize
  • admin
  • model
  • fixtures
  • tests
  • test
  • management
  • commands
  • fixture
  • command
  • make
Read More

JSON encoding middleware

Makes it really easy to return JSON from your views: just return a dict. (Also from [django-webapp](http://code.google.com/p/django-webapp/).)

  • middleware
  • serialize
  • json
  • encode
  • encoding
  • serializer
Read More

Extended JSON encoder

The Django JSON encoder already extends the `simplejson` encoder a little; this extends it more and gives an example of how to go about further extension. Hopefully `newserializers` (see the community aggregator today) will supercede this, but until then, it's useful.

  • serialize
  • json
  • serializer
Read More

JsonResponse

A subclass of `HttpResponse` useful as a shortcut in views; it chooses the correct JSON serializer based on whether or not it is passed a QuerySet.

  • ajax
  • serialize
  • json
Read More
Author: zakj
  • 24
  • 85

ajax protocol for data

Can be used for create a json format response data. Just like: {response:'ok', next:'nexturl', message:'response message', data:'returned data'} for success. {response:'fail', next:'nexturl', message:'response message', error:'error messages'} for failure. And there are some dump function: json - dump a python variable into json format string json_response - dump a python variable into json format string, and then use it create a HttpResponse object.

  • ajax
  • serialize
Read More

12 snippets posted so far.