MCPcopy
hub / github.com/encode/django-rest-framework / as_view

Method as_view

rest_framework/views.py:123–150  ·  view source on GitHub ↗

Store the original class on the view function. This allows us to discover information about the view when we do URL reverse lookups. Used for breadcrumb generation.

(cls, **initkwargs)

Source from the content-addressed store, hash-verified

121
122 @classmethod
123 def as_view(cls, **initkwargs):
124 """
125 Store the original class on the view function.
126
127 This allows us to discover information about the view when we do URL
128 reverse lookups. Used for breadcrumb generation.
129 """
130 if isinstance(getattr(cls, 'queryset', None), models.query.QuerySet):
131 def force_evaluation():
132 raise RuntimeError(
133 'Do not evaluate the `.queryset` attribute directly, '
134 'as the result will be cached and reused between requests. '
135 'Use `.all()` or call `.get_queryset()` instead.'
136 )
137 cls.queryset._fetch_all = force_evaluation
138
139 view = super().as_view(**initkwargs)
140 view.cls = cls
141 view.initkwargs = initkwargs
142
143 # Exempt all DRF views from Django's LoginRequiredMiddleware. Users should set
144 # DEFAULT_PERMISSION_CLASSES to 'rest_framework.permissions.IsAuthenticated' instead
145 if DJANGO_VERSION >= (5, 1):
146 view.login_required = False
147
148 # Note: session based authentication is explicitly CSRF validated,
149 # all other authentication is CSRF exempt.
150 return csrf_exempt(view)
151
152 @property
153 def allowed_methods(self):

Calls

no outgoing calls