-
-
Save moradology/156a7a01808d48703c58 to your computer and use it in GitHub Desktop.
Subclass F objects to provide JSONB field access in Django 1.8+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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