Skip to content

Instantly share code, notes, and snippets.

@moradology
Forked from ddohler/extensions.py
Created September 29, 2015 17:38
Show Gist options
  • Save moradology/156a7a01808d48703c58 to your computer and use it in GitHub Desktop.
Save moradology/156a7a01808d48703c58 to your computer and use it in GitHub Desktop.
Subclass F objects to provide JSONB field access in Django 1.8+
from django.db.models import F
# Needs expansion but may eventually allow things like:
# recs = Record.objects.annotate(num_killed=JBF('data').jbPath(Value("{Accident Details, Number killed}"))).filter(num_killed__asint=1)
class JBF(F):
JB_PATH = '#>'
JB_PATH_STR = '#>>'
JB_FIELD = '->'
JB_FIELD_STR = '->>'
JB_ELEM = '->'
JB_ELEM_STR = '->'
def jbPath(self, other):
return self._combine(other, self.JB_PATH, False)
def jbPathStr(self, other):
return self._combine(other, self.JB_PATH_STR, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment