Skip to content

Instantly share code, notes, and snippets.

@ddohler
Created September 29, 2015 17:34
Show Gist options
  • Save ddohler/c3c3a96cbaa247c9f314 to your computer and use it in GitHub Desktop.
Save ddohler/c3c3a96cbaa247c9f314 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