def get_object(self):
query = self.filter_query(self.get_query())
# If query joins more than one table and you need to base the lookup on something besides
# an id field on the self.model, you can provide an alternative lookup as tuple of the model class
# and a string of the column name.
if isinstance(self.lookup_field, str):
lookup_col = getattr(self.model, self.lookup_field)
lookup_val = self.lookup_url_kwargs[self.lookup_field]
else:
assert isinstance(self.lookup_field, tuple), (
"'{}' `lookup_field` attribute should be a string or a tuple of (<model class>, `column`) "
.format(self.__class__.__name__)
)
lookup_col = getattr(self.lookup_field[0], self.lookup_field[1])
lookup_val = self.lookup_url_kwargs[self.lookup_field[1]]
try:
instance = query.filter(lookup_col == lookup_val).one()
except NoResultFound:
raise HTTPNotFound()
# May raise HTTPForbidden
self.check_object_permissions(self.request, instance)
return instance
评论列表
文章目录