File tree Expand file tree Collapse file tree 2 files changed +51
-2
lines changed Expand file tree Collapse file tree 2 files changed +51
-2
lines changed Original file line number Diff line number Diff line change @@ -356,8 +356,15 @@ def metadata(self, request):
356
356
self .check_permissions (cloned_request )
357
357
# Test object permissions
358
358
if method == 'PUT' :
359
- self .get_object ()
360
- except (exceptions .APIException , PermissionDenied , Http404 ):
359
+ try :
360
+ self .get_object ()
361
+ except Http404 :
362
+ # Http404 should be acceptable and the serializer
363
+ # metadata should be populated. Except this so the
364
+ # outer "else" clause of the try-except-else block
365
+ # will be executed.
366
+ pass
367
+ except (exceptions .APIException , PermissionDenied ):
361
368
pass
362
369
else :
363
370
# If user has appropriate permissions for the view, include
Original file line number Diff line number Diff line change @@ -272,6 +272,48 @@ def test_options_instance_view(self):
272
272
self .assertEqual (response .status_code , status .HTTP_200_OK )
273
273
self .assertEqual (response .data , expected )
274
274
275
+ def test_options_before_instance_create (self ):
276
+ """
277
+ OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata
278
+ before the instance has been created
279
+ """
280
+ request = factory .options ('/999' )
281
+ with self .assertNumQueries (1 ):
282
+ response = self .view (request , pk = 999 ).render ()
283
+ expected = {
284
+ 'parses' : [
285
+ 'application/json' ,
286
+ 'application/x-www-form-urlencoded' ,
287
+ 'multipart/form-data'
288
+ ],
289
+ 'renders' : [
290
+ 'application/json' ,
291
+ 'text/html'
292
+ ],
293
+ 'name' : 'Instance' ,
294
+ 'description' : 'Example description for OPTIONS.' ,
295
+ 'actions' : {
296
+ 'PUT' : {
297
+ 'text' : {
298
+ 'max_length' : 100 ,
299
+ 'read_only' : False ,
300
+ 'required' : True ,
301
+ 'type' : 'string' ,
302
+ 'label' : 'Text comes here' ,
303
+ 'help_text' : 'Text description.'
304
+ },
305
+ 'id' : {
306
+ 'read_only' : True ,
307
+ 'required' : False ,
308
+ 'type' : 'integer' ,
309
+ 'label' : 'ID' ,
310
+ },
311
+ }
312
+ }
313
+ }
314
+ self .assertEqual (response .status_code , status .HTTP_200_OK )
315
+ self .assertEqual (response .data , expected )
316
+
275
317
def test_get_instance_view_incorrect_arg (self ):
276
318
"""
277
319
GET requests with an incorrect pk type, should raise 404, not 500.
You can’t perform that action at this time.
0 commit comments