def try_to_get(obj, pk):
"""
This method can be used to get a model instance based on it's primary key.
If the instance does not exists, it will automatically throws a 404 error.
Method version : uses the queryset provided in the subclass definition
Static version : takes the queryset to use as its first argument.
"""
if isinstance(obj, SigmaViewSet):
return SigmaViewSet.try_to_get(obj.__class__.queryset, pk)
try:
instance = obj.get(pk=pk)
except obj.model.DoesNotExist:
raise NotFound()
return instance
评论列表
文章目录