Skip to content

Commit 9b5d0e1

Browse files
committed
add association query one to many support
1 parent d746fb2 commit 9b5d0e1

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

demo/apps/apijson_demo/views.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ def index():
122122
"email$":"%local%"
123123
}
124124
}
125+
}''',
126+
},
127+
{
128+
"label":"Array query: association query one to many",
129+
"value":'''{
130+
"moment": {},
131+
"[]": {
132+
"comment": {
133+
"moment_id@": "moment/id"
134+
}
135+
}
125136
}''',
126137
},
127138
{

uliweb_apijson/apijson/views.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def get(self):
5555
rsp = self._get_one(key)
5656
if rsp: return rsp
5757
self._apply_vars()
58+
except UliwebError as e:
59+
return json({"code":400,"msg":str(e)})
5860
except Exception as e:
5961
err = "exception when handling 'apijson get': %s"%(e)
6062
log.error(err)
@@ -230,6 +232,20 @@ def _get_array(self,key):
230232

231233
model_expr = model_param.get("@expr")
232234

235+
#update reference
236+
ref_fields = []
237+
refs = {}
238+
for n in model_param:
239+
if n[-1]=="@":
240+
ref_fields.append(n)
241+
col_name = n[:-1]
242+
path = model_param[n]
243+
refs[col_name] = self._ref_get(path)
244+
if ref_fields:
245+
for i in ref_fields:
246+
del model_param[i]
247+
model_param.update(refs)
248+
233249
if model_expr:
234250
c = self._expr(model,model_param,model_expr)
235251
q = q.filter(c)

0 commit comments

Comments
 (0)