djangosnippets.org: Latest snippets tagged with 'json'https://djangosnippets.org/tags/json/2024-01-05T14:28:02.916197-06:00JSONRequestMiddleware adds a .json() method to your HttpRequests
2024-01-05T14:28:02.916197-06:00cdcarterhttps://djangosnippets.org/snippets/10951/<p>add JSONRequestMiddleware to your enabled middleware in Django settings. Now, in your view functions, you can call request.json() to get a parsed json body! json is consumed lazily, and cached.</p>
Freely redistributablePostgreSQL JSON subqueries
2021-08-26T06:45:03.783842-05:00dolamrothhttps://djangosnippets.org/snippets/10860/<h4>Allows to fetch a row or array of rows of data, linked to parent object, in a single query. Data is fetched as JSON and is not serialized into Django objects.</h4>
<h5>Example:</h5>
<pre><code>from django.db import Models
class Book(models.Model):
authors = models.ManyToMany('Author', through='BookToAuthor', blank=True)
title = models.CharField(max_length=512, default='')
class Author(models.Model):
name …</code></pre>
Freely redistributableparse json string
2019-08-11T17:23:52.066397-05:00agusmakmunhttps://djangosnippets.org/snippets/10676/<p>function to parse the string text into json format</p>
Freely redistributablespaceless_json
2019-03-04T06:53:58.732628-06:00dkoldyaevhttps://djangosnippets.org/snippets/10661/<p>Now you can format and compress json-data in django template</p>
Freely redistributableTweet embed template tag
2019-02-14T14:22:23.966059-06:00z3ke1rhttps://djangosnippets.org/snippets/10660/<p>Takes a tweet url, requests the json from Twitter oEmbed, parses the json for the html element and returns it to your template. The html returned is ready to go and will be shown as a tweet on your web page. This uses the Requests library for Python. A full …</p>
Freely redistributableGoogle URL Shortner using Python requests
2015-05-17T20:31:21.974850-05:00akshayw39https://djangosnippets.org/snippets/10486/<p>This code can convert a url to short url using google URL Shortener API https://developers.google.com/url-shortener/v1/getting_started </p>
Freely redistributableQSerializer with default to JSON input and output
2013-11-09T00:46:49.626188-06:00asfaltboyhttps://djangosnippets.org/snippets/3003/<p>A module to serializers/deserializes Django Q (query) object</p>
Freely redistributableA widget that renders JSON data as separate editable inputs.
2013-07-16T19:53:06.692353-05:00abbasovalexhttps://djangosnippets.org/snippets/2974/<p>You can see full examples on the page <a href="https://github.com/abbasovalex/django-SplitJSONWidget-form">GitHub</a></p>
Freely redistributableSerialize all models from tastypie resource
2013-03-08T11:22:11.660306-06:00xtranophilisthttps://djangosnippets.org/snippets/2900/<p>Serializes all models of a resource to json.</p>
<p>Borrowed from https://github.com/toastdriven/django-tastypie/issues/442</p>
Freely redistributableJSON decorator for views handling ajax requests
2012-12-23T11:05:05.312451-06:00anilshanbhaghttps://djangosnippets.org/snippets/2869/<p>Sample usage for using decorator
<code>@json_response(ajax_required=True, login_required=True)</code>
<code>def subscribe(request):</code>
<code>return {"status":"success"}</code></p>
<p>Converts a function returning dict into json response. Does is_ajax check and user authenticated check if set in flags. When function returns HttpResponse does nothing.</p>
Freely redistributableSyndication Feed for JSON
2012-12-05T07:19:28.849784-06:00catohttps://djangosnippets.org/snippets/2859/<p>This feed class outputs an existing xml feed as json</p>
Freely redistributableJquery ajax csrf framework for Django
2012-01-23T00:30:21.791394-06:00chriszweberhttps://djangosnippets.org/snippets/2656/<ol>
<li>
<p>Framework to extend the jquery ajax() function to construct post requests that contain a csrf token.</p>
</li>
<li>
<p>The example view used with the framework takes JSON data and returns JSON data containing either:</p>
</li>
<li>"success" with a message and additional dictionary of JSON data to use in the page</li>
<li>
<p>"error" with an …</p></li></ol>
Freely redistributableJSON instead of pickle for memcached
2012-01-10T16:21:06.401532-06:00divermanhttps://djangosnippets.org/snippets/2642/<p>Standard memcache client uses pickle as a serialization format. It can be handy to use json, especially when another component (e.g. backend) doesn't know pickle, but json yes.</p>
Freely redistributablecreate or update, then get, model instances from JSON/py dict
2011-12-05T07:50:10.129560-06:00anentropichttps://djangosnippets.org/snippets/2621/<p>Basically the idea is to import/update model instances from a json data that closely matches the model structure (i.e. identical field names)</p>
<p>From my answer to this question: <a href="http://stackoverflow.com/a/8377382/202168">http://stackoverflow.com/a/8377382/202168</a></p>
<p>See the original question for sample data format.</p>
Freely redistributableRemove old fields on dumpdata generated json
2011-10-21T15:46:38.312378-05:00facundo_olanohttps://djangosnippets.org/snippets/2576/<p>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.</p>
Freely redistributable