Assume composite PK when fetching instance for master view
i.e. stop trying a simple get() which would assume not only a simple PK, but also assumes the PK is same as defined by the class mapper. in some cases it may be helpful to use a different PK from what mapper defines
This commit is contained in:
parent
0220e401cd
commit
a204e78e3a
|
@ -3658,28 +3658,23 @@ class MasterView(View):
|
||||||
doing a database lookup. If the instance cannot be found, raises 404.
|
doing a database lookup. If the instance cannot be found, raises 404.
|
||||||
"""
|
"""
|
||||||
model_keys = self.get_model_key(as_tuple=True)
|
model_keys = self.get_model_key(as_tuple=True)
|
||||||
|
query = self.Session.query(self.get_model_class())
|
||||||
|
|
||||||
# if just one primary key, simple get() will work
|
def filtr(query, model_key):
|
||||||
if len(model_keys) == 1:
|
|
||||||
model_key = model_keys[0]
|
|
||||||
key = self.request.matchdict[model_key]
|
key = self.request.matchdict[model_key]
|
||||||
|
if self.key_is_integer(model_key):
|
||||||
|
key = int(key)
|
||||||
|
query = query.filter(getattr(self.model_class, model_key) == key)
|
||||||
|
return query
|
||||||
|
|
||||||
obj = self.Session.query(self.get_model_class()).get(key)
|
# filter query by composite key. we use filter() instead of a simple
|
||||||
if not obj:
|
# get() here in case view uses a "pseudo-PK"
|
||||||
raise self.notfound()
|
for i, model_key in enumerate(model_keys):
|
||||||
|
query = filtr(query, model_key)
|
||||||
else: # composite key; fetch accordingly
|
try:
|
||||||
# TODO: should perhaps use filter() instead of get() here?
|
obj = query.one()
|
||||||
query = self.Session.query(self.get_model_class())
|
except orm.exc.NoResultFound:
|
||||||
for i, model_key in enumerate(model_keys):
|
raise self.notfound()
|
||||||
key = self.request.matchdict[model_key]
|
|
||||||
if self.key_is_integer(model_key):
|
|
||||||
key = int(key)
|
|
||||||
query = query.filter(getattr(self.model_class, model_key) == key)
|
|
||||||
try:
|
|
||||||
obj = query.one()
|
|
||||||
except orm.exc.NoResultFound:
|
|
||||||
raise self.notfound()
|
|
||||||
|
|
||||||
# pretend global object doesn't exist, unless access allowed
|
# pretend global object doesn't exist, unless access allowed
|
||||||
if self.secure_global_objects:
|
if self.secure_global_objects:
|
||||||
|
|
Loading…
Reference in a new issue