Snippet List
Perhaps this is blindingly obvious but I spent a while trying to get this, before figuring out that when a each item of an iterated ValuesQuerySet is a dict.
Useful for serialising to JSON or XML etc.
- json
- serialization
- valuesqueryset
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
Warning: This python script is designed for Django 0.96.
It exports data from models quite like the `dumpdata` command, and throws the
data to the standard output.
It fixes glitches with unicode/ascii characters. It looked like the 0.96
handles very badly unicode characters, unless you specify an argument that is
not available via the command line. The simple usage is:
$ python export_models.py -a <application1> [application2, application3...]
As a plus, it allows you to export only one or several models inside your
application, and not all of them:
$ python export_models.py application1.MyModelStuff application1.MyOtherModel
Of course, you can specify the output format (serializer) with the -f
(--format) option.
$ python export_models.py --format=xml application1.MyModel
- tool
- dump
- serialization
- export
- db
- database
This creates a fixture in the form of a python script.
Handles:
1. `ForeignKey` and `ManyToManyField`s (using python variables, not IDs)
2. Self-referencing `ForeignKey` (and M2M) fields
3. Sub-classed models
4. `ContentType` fields
5. Recursive references
6. `AutoField`s are excluded
7. Parent models are only included when no other child model links to it
There are a few benefits to this:
1. edit script to create 1,000s of generated entries using `for` loops, python modules etc.
2. little drama with model evolution: foreign keys handled naturally without IDs, new and removed columns are ignored
The [runscript command by poelzi](http://code.djangoproject.com/ticket/6243), complements this command very nicely! e.g.
$ ./manage.py dumpscript appname > scripts/testdata.py
$ ./manage.py reset appname
$ ./manage.py runscript testdata
- dump
- manage.py
- serialization
- fixtures
- migration
- data
- schema-evolution
- management
- commands
- command
A serializer that handles dicts with querysets and model instances, nice to use if you have a paginator context.
paginate_by = 8
paginator = ObjectPaginator(queryset, paginate_by)
page = request.GET.get('page', 1)
try:
page = int(page)
object_list = paginator.get_page(page - 1)
except (InvalidPage, ValueError):
if page == 1 and allow_empty:
object_list = []
else:
raise Http404
result = {
'object_list' : object_list,
'is_paginated': paginator.pages > 1,
'results_per_page': paginate_by,
'has_next': paginator.has_next_page(page - 1),
'has_previous': paginator.has_previous_page(page - 1),
'page': page,
'next': page + 1,
'previous': page - 1,
'pages': paginator.pages,
'hits' : paginator.hits,
}
serialize(result, ensure_ascii=False)
5 snippets posted so far.