Snippet List
Adds `--pretty` option to django `./manage.py dumpdata` command, which produces pretty utf-8 strings instead of ugly unicode-escaped s**t:
> $ ./manage.py dumpdata app.pricingplan --indent=1
<pre>
<code>[
{
"pk": 1,
"model": "app.pricingplan",
"fields": {
"name": "\u0411\u0430\u0437\u043e\u0432\u044b\u0439",
}
},
{
"pk": 2,
"model": "app.pricingplan",
"fields": {
"name": "\u0425\u0443\u044f\u0437\u043e\u0432\u044b\u0439",
}
}
]
</code>
</pre>
> ./manage.py dumpdata app.pricingplan --indent=1 --pretty
<pre>
<code>[
{
"pk": 1,
"model": "app.pricingplan",
"fields": {
"name": "Базовый",
}
},
{
"pk": 2,
"model": "app.pricingplan",
"fields": {
"name": "Хуязовый",
}
}
]
</code>
</pre>
Forked from an [old versions snippet](https://djangosnippets.org/snippets/2258/)
- pretty
- dumpdata
- pretty-print
A small script that takes a manage.py dumpdata generated json file, and removes fields of the specified models. I needed this because i kept my initial data on a json file and after I removed a field on one of my models, the script wouldn't work anymore.
- json
- loaddata
- fixture
- dumpdata
- migrate
- removed field
Adds `--pretty` option to django `./manage.py dumpdata` command, which produces pretty utf8 strings instead of ugly unicode-escaped shit:
$ ./manage.py dumpdata app.pricingplan --indent=1
[
{
"pk": 1,
"model": "app.pricingplan",
"fields": {
"name": "\u0411\u0430\u0437\u043e\u0432\u044b\u0439",
}
},
{
"pk": 2,
"model": "app.pricingplan",
"fields": {
"name": "\u0425\u0443\u044f\u0437\u043e\u0432\u044b\u0439",
}
}
]%
./manage.py dumpdata app.pricingplan --indent=1 --pretty
[
{
"pk": 1,
"model": "app.pricingplan",
"fields": {
"name": "Базовый",
}
},
{
"pk": 2,
"model": "app.pricingplan",
"fields": {
"name": "Хуязовый",
}
}
]%
- fixtures
- management
- dumpdata
***About***
I tried to dump data from my database (manage.py dumpdata) and I couldn't do it because of error:
User matching query does not exists
I found out that my database was filled with garbage: entries those foreigners were deleted. My table's engine is MyISAM so it allows for these lost entries to exist. I had to cleanup my database before I do datadump, so I've written a script which worked fine for me.
***Usage***
Place this script in same directory with your settings.py and run it: `python db_cleanup.py`
***Disclaimer***
Backup your data :)
Based on [http://www.djangosnippets.org/snippets/662/](http://www.djangosnippets.org/snippets/662/) and updated to be runnable as custom django management command. Also added option support for --exclude=someapp --exclude=otherapp.SomeModel
From original description:
InnoDB tables within MySQL have no ability to defer reference checking until after a transaction is complete. This prevents most dumpdata/loaddata cycles unless the dump order falls so that referenced models are dumped before models that depend on them.
Caveats
1. You use this snippet to dump the data and the built in manage.py loaddata to load the fixture output by this program. A similar solution could be applied to the XML processing on the loaddata side but this sufficed for my situations.
2. This code does not handle Circular or self-references. The loaddata for those needs to be much smarter
- mysql
- fixtures
- dumpdata
- command
`dumpdata` without `MemoryErrors`, with progress notification. Most of the real work is done by snippets [1400](http://www.djangosnippets.org/snippets/1400/) and [1401](http://www.djangosnippets.org/snippets/1401/).
./manage.py dumpdata_stream --format=xml > big_dump.xml
This is basically the stock Django `dumpdata` with a few modifications. Django devs: it's hard to reuse parts of most Django management commands. A little refactoring could go a long way.
- dumpdata
- memoryerror
- stream
- dump_data
- queryset_foreach
Trying `./manage.py dumpdata` on a huge database and getting `MemoryError`s? Here's part of your solution.
[Snippet 1400](http://www.djangosnippets.org/snippets/1400/) provides a queryset_foreach utility that we've found very useful. This snippet uses it on a serializer that can output to a stream, such as the XML serializer.
Management command coming momentarily...
- dumpdata
- large
- memoryerror
Django 0.96 seems to have a bug when serializing from MySQL. BooleanFields are encoding as 0/1 instead of true/false. Hacking the python serializer seems to fix that.
The bug shows up as (fx. when using loaddata on a dump from MySQL in PostgreSQL):
Problem installing fixture '/tmp/data.json': ERROR: column "is_staff" is of
type boolean but expression is of type integer
HINT: You will need to rewrite or cast the expression.
- dumpdata
- 0.96
- seralization
nnoDB tables within MySQL have no ability to defer reference checking until after a transaction is complete. This prevents most dumpdata/loaddata cycles unless the dump order falls so that referenced models are dumped before models that depend on them.
This code uses Ofer Faigon's topological sort to sort the models so that any models with a ForeignKey relationship are dumped after the models they reference.
class Entry(models.Model):
txt = ....
class Comment(models.Model):
entry = models.ForeignKey(Entry)
This code will ensure that Entry always gets dumped before Comment.
Fixtures are an important part of the django Unit Testing framework so I really needed to be able to test my more complicated models.
Caveats
1.
You use this snippet to dump the data and the built in manage.py loaddata to load the fixture output by this program. A similar solution could be applied to the XML processing on the loaddata side but this sufficed for my situations.
2.
This code does not handle Circular or self-references. The loaddata for those needs to be much smarter.
InnoDB tables within MySQL have no ability to defer reference checking until after a transaction is complete. This prevents most dumpdata/loaddata cycles unless the dump order falls so that referenced models are dumped before models that depend on them.
This code uses [Ofer Faigon's](http://www.bitformation.com) topological sort to sort the models so that any models with a ForeignKey relationship are dumped after the models they reference.
class Entry(models.Model):
txt = ....
class Comment(models.Model):
entry = models.ForeignKey(Entry)
This code will ensure that Entry always gets dumped before Comment.
Fixtures are an important part of the django Unit Testing framework so I really needed to be able to test my more complicated models.
**Caveats**
1. You use this snippet to dump the data and the built in manage.py loaddata to load the fixture output by this program. A similar solution could be applied to the XML processing on the loaddata side but this sufficed for my situations.
2. This code does not handle Circular or self-references. The loaddata for those needs to be much smarter.
- mysql
- loaddata
- foreign-key
- fixture
- dumpdata
15 snippets posted so far.