TCS Wings 1 T4 Django - Set 3
- You are developing a Django application that requires users to be authenticated to access specific pages. How do you enforce authentication for a particular view?
a) Use the@login_requireddecorator
b) Override thesave()method in the view
c) Use a custom form validation method
d) Implement session management inurls.py
Answer: a) Use the @login_required decorator
- Your Django application uses JWT tokens for authentication. You need to allow users to refresh their JWT tokens after they expire. Which token type is used for this purpose?
a) Access token
b) Refresh token
c) ID token
d) Authorization token
Answer: b) Refresh token
- You are working on a Django Rest Framework API where a user can upload a file. You want to restrict the file size and type. How can you enforce this in your API?
a) Override thecreate()method in the view
b) Implement validation in the serializer
c) Restrict it in the model definition
d) Handle it in the frontend JavaScript
Answer: b) Implement validation in the serializer
- You need to implement a filter in Django Rest Framework that allows users to filter a list of products by price range. How would you add this filtering logic?
a) UseSearchFilterin the view
b) Define a custom filter in the serializer
c) Use DjangoFilterBackend and specify the filter fields in the view
d) Implement the filter logic directly in the model
Answer: c) Use DjangoFilterBackend and specify the filter fields in the view
- In your Django project, you want to limit a queryset based on the logged-in user’s permissions. What’s the best way to handle this?
a) Usefilter()with a custom permission decorator
b) Overrideget_queryset()in the view
c) Implement it inurls.py
d) UseManyToManyFieldfor permissions in the model
Answer: b) Override get_queryset() in the view
- Your Django Rest Framework API provides detailed information about a book when given the book's ID. Which method in DRF views should you use to handle GET requests for this specific resource?
a)list()
b)retrieve()
c)create()
d)update()
Answer: b) retrieve()
- In a Django application, you want to prefetch related objects to reduce the number of database queries. Which Django ORM method should you use to achieve this?
a)select_related()
b)annotate()
c)get_queryset()
d)values()
Answer: a) select_related()
- Your Django application needs to log out a user when their JWT token expires. Which setting in
django-rest-framework-simplejwthandles automatic token expiration and refresh?
a)TOKEN_REFRESH_LIFETIME
b)ROTATE_REFRESH_TOKENS
c)ACCESS_TOKEN_LIFETIME
d)JWT_EXPIRATION
Answer: c) ACCESS_TOKEN_LIFETIME
- You are building an API in Django Rest Framework to handle image uploads. How can you limit the maximum file size and validate the image format?
a) UseImageFieldwithmax_length
b) Define custom validators in the serializer
c) Limit file size in theviews.py
d) UseFileFieldwithmax_length
Answer: b) Define custom validators in the serializer
- You want to allow users to perform a partial update on a resource in Django Rest Framework. Which method should be used to handle partial updates in DRF views?
a)post()
b)patch()
c)put()
d)get()
Answer: b) patch()

Comments
Post a Comment