TCS Wings 1 T4 Django - Set 3



  1. 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_required decorator
    b) Override the save() method in the view
    c) Use a custom form validation method
    d) Implement session management in urls.py

Answer: a) Use the @login_required decorator


  1. 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


  1. 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 the create() 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

  1. 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) Use SearchFilter in 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


  1. 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) Use filter() with a custom permission decorator
    b) Override get_queryset() in the view
    c) Implement it in urls.py
    d) Use ManyToManyField for permissions in the model

Answer: b) Override get_queryset() in the view


  1. 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()


  1. 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()


  1. Your Django application needs to log out a user when their JWT token expires. Which setting in django-rest-framework-simplejwt handles 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


  1. 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) Use ImageField with max_length
    b) Define custom validators in the serializer
    c) Limit file size in the views.py
    d) Use FileField with max_length

Answer: b) Define custom validators in the serializer


  1. 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

Popular posts from this blog

TCS Wings 1 T4 Django - Set 1

TCS Wings 1 T4 Django - Set 10

TCS Wings 1 T4 Django - Set 4