Skip to content

Commit 03ec9fe

Browse files
committed
Make view a property
Encapsulates check for a view instance.
1 parent fbbdc38 commit 03ec9fe

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

rest_framework/schemas.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,23 @@ class APIViewSchemaDescriptor(object):
263263
Responsible for per-view instrospection and schema generation.
264264
"""
265265
def __get__(self, instance, owner):
266-
# ???: Is this TOO simple? (Option is to return a new instance each time.)
267266
self.view = instance
268267
return self
269268

269+
@property
270+
def view(self):
271+
"""View property."""
272+
assert self._view is not None, "Schema generation REQUIRES a view instance. (Hint: you accessed `schema` from the view class rather than an instance.)"
273+
return self._view
274+
275+
@view.setter
276+
def view(self, value):
277+
self._view = value
278+
279+
@view.deleter
280+
def view(self):
281+
self._view = None
282+
270283
def get_link(self, path, method, base_url):
271284
"""
272285
Generate `coreapi.Link` for self.view, path and method.
@@ -279,10 +292,6 @@ def get_link(self, path, method, base_url):
279292
* method: The HTTP request method.
280293
* base_url: The project "mount point" as given to SchemaGenerator
281294
"""
282-
# TODO: make `view` a property: move this check to getter.
283-
assert self.view is not None, "Schema generation REQUIRES a view instance. (Hint: you accessed `schema` from the view CLASS rather than an instance.)"
284-
view = self.view
285-
286295
fields = self.get_path_fields(path, method)
287296
fields += self.get_serializer_fields(path, method)
288297
fields += self.get_pagination_fields(path, method)

0 commit comments

Comments
 (0)