Login

Tag "json"

Snippet List

PostgreSQL JSON subqueries

#### 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. ##### Example: 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 = models.CharField(max_length=512, default='') class BookToAuthor(models.Model): author = models.ForeignKey(Author, on_delete=models.CASCADE) book = models.ForeignKey(Book, on_delete=models.CASCADE) ##### Download author with all his/her books in a single query from django.db.models import OuterRef books_by_author_subquery = Book.objects.filter( id__in=BookToAuthor.objects.filter(author_id=OuterRef(OuterRef('id'))) ).values('title') author = Author.objects\ .annotate(books=SubqueryJsonAgg(books_by_author_subquery))\ .get(id=1)

  • json
  • postgres
  • postgresql
  • subquery
Read More

spaceless_json

Now you can format and compress json-data in django template

  • django
  • templatetag
  • json
  • spaceless
  • formatting
  • application/id+json
Read More

Tweet embed template tag

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 example can be found on GitHub https://github.com/z3ke1r/django-tweet-embed.

  • django
  • templatetag
  • json
  • embed
  • twitter
  • parse
  • requests
  • tweet
Read More

JSON decorator for views handling ajax requests

Sample usage for using decorator `@json_response(ajax_required=True, login_required=True)` `def subscribe(request):` ` return {"status":"success"}` 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.

  • ajax
  • json
  • decorator
  • jsonp
Read More

Jquery ajax csrf framework for Django

1. Framework to extend the jquery ajax() function to construct post requests that contain a csrf token. 2. The example view used with the framework takes JSON data and returns JSON data containing either: 3. "success" with a message and additional dictionary of JSON data to use in the page 4. "error" with an error message. 5. The ajax function framework satisfies Django's csrf requirements by injecting a csrf token into the post requests created using the function. This example is a form with ~160 fields that we wanted to help fill in customer information to automatically. 1. User calls the lookup() script from the onblur attribute of the customer_id form field by leaving the field. 2. The lookup script takes the contents of the customer_id formfield and uses the jquery ajax() function to construct a JSON post request to the "/json /?act=" url. 3. The json view takes actions as get requests. We pass the post request to the JSON url already including the get request. "/json/?act=lookup" 4. The jquery framework in the snippet includes a csrf token in the ajax request automatically. 5. The customer_id is passed as JSON to the json view lookup action and customer details are attempted to be looked up in the database. 6. If successful the request returns a JSON dictionary of customer details which are pushed into the formfields using javascript in the lookup() function. The end result is if the user fills out the customer_id field of the form first (which we suggest with tooltip overlay) the customer name and address information will populate automatically. *Credit to Guangcong Luo https://github.com/Zarel

  • json
  • jquery
  • csrf
Read More

JSON instead of pickle for memcached

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.

  • memcache
  • cache
  • json
  • memcached
  • pickle
Read More

create or update, then get, model instances from JSON/py dict

Basically the idea is to import/update model instances from a json data that closely matches the model structure (i.e. identical field names) From my answer to this question: [http://stackoverflow.com/a/8377382/202168](http://stackoverflow.com/a/8377382/202168) See the original question for sample data format.

  • json
  • import
Read More

Remove old fields on dumpdata generated json

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
Read More

62 snippets posted so far.