Source

django-jsonfield /

Filename Size Date modified Message
jsonfield
69 B
29 B
1.7 KB
562 B

django-jsonfield

I had a serious need for a JSON field for django. There were a couple out there, but none packaged up nicely on bitbucket/github that were usable with pip install -e.

So I took the code from David Cramer's blog, and packaged it up.

To use, just install the package, and then use the field:

from django.db import models
import jsonfield

class MyModel(models.Model):
    the_json = jsonfield.JSONField()

Now, it will validate the JSON on entry, and store it as a string in the database. When you instantiate/fetch the object, it will be turned back into a python list/dict/string.

There are also a couple of other bits and bobs:

  • jsonify templatetag: This allows you to convert a python data structure into JSON within a template.

    {% load jsonify %}

    <script> var foo = {{ bar|jsonify }}; </script>

  • JSONTableWidget I strongly recommend you don't use this: it's an experimental way of having a generic JSON structure represented as a table, enabling you to let non-programmer types edit JSON data. I have replaced a lot of the code that is here with some new KnockoutJS-based stuff, and that simplifies it a whole lot. As such, I am not using this widget any longer, and it may not work.

History

  • 0.7 : First time I tagged releases.

Todo

Convert date/time objects nicely to/from ISO strings (YYYY-mm-dd HH:MM:SS TZNAME). This is actually a bit tricky, as we don't know if we are expecting a date/time object. We may parse objects as we go, but there could be some performance issues with this.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.