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,24 +3658,19 @@ class MasterView(View):
|
|||
doing a database lookup. If the instance cannot be found, raises 404.
|
||||
"""
|
||||
model_keys = self.get_model_key(as_tuple=True)
|
||||
|
||||
# if just one primary key, simple get() will work
|
||||
if len(model_keys) == 1:
|
||||
model_key = model_keys[0]
|
||||
key = self.request.matchdict[model_key]
|
||||
|
||||
obj = self.Session.query(self.get_model_class()).get(key)
|
||||
if not obj:
|
||||
raise self.notfound()
|
||||
|
||||
else: # composite key; fetch accordingly
|
||||
# TODO: should perhaps use filter() instead of get() here?
|
||||
query = self.Session.query(self.get_model_class())
|
||||
for i, model_key in enumerate(model_keys):
|
||||
|
||||
def filtr(query, 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
|
||||
|
||||
# filter query by composite key. we use filter() instead of a simple
|
||||
# get() here in case view uses a "pseudo-PK"
|
||||
for i, model_key in enumerate(model_keys):
|
||||
query = filtr(query, model_key)
|
||||
try:
|
||||
obj = query.one()
|
||||
except orm.exc.NoResultFound:
|
||||
|
|
Loading…
Reference in a new issue