TCS Wings 1 T4 Django - Set 8


  1. In Django ORM, how can you prefetch related objects to optimize database queries for many-to-many relationships?
    a) select_related()
    b) annotate()
    c) values()
    d) prefetch_related()

Answer: d) prefetch_related()


  1. You want to define custom actions for a Django Rest Framework ModelViewSet. Where should you define these custom actions?
    a) In the serializer
    b) In the urls.py file
    c) In the viewset using @action decorator
    d) In the model

Answer: c) In the viewset using @action decorator


  1. Your Django project needs to send a confirmation email to users after they register. Which Django feature would you use to send emails?
    a) Django Celery
    b) Django Signals
    c) Django Email Backend
    d) Django Tasks

Answer: c) Django Email Backend


  1. You are implementing a JWT-based authentication system for your API. How would you securely store the JWT token on the client-side?
    a) Store it in local storage
    b) Store it in a session cookie with HttpOnly flag
    c) Include it in the URL parameters
    d) Store it in a regular cookie

Answer: b) Store it in a session cookie with HttpOnly flag


  1. You are building a REST API that needs to serve both JSON and XML responses based on client requests. How do you handle this in Django Rest Framework?
    a) Use ContentNegotiation class
    b) Override get_serializer() method
    c) Set DEFAULT_RENDERER_CLASSES in settings.py
    d) Use accept() header in views

Answer: c) Set DEFAULT_RENDERER_CLASSES in settings.py


  1. You are building a Django application that uses multiple databases. How do you configure Django to route queries to different databases?
    a) Use DATABASES setting in settings.py
    b) Use db_for_read and db_for_write in the model
    c) Define separate models for each database
    d) Use Django's router.py

Answer: a) Use DATABASES setting in settings.py


  1. You want to ensure that only authenticated users can create new objects in your Django Rest Framework API, but allow unauthenticated users to view objects. How can you achieve this?
    a) Use AllowAny permission
    b) Use IsAuthenticated for POST requests and AllowAny for GET
    c) Use IsAuthenticated for all requests
    d) Override the perform_create() method

Answer: b) Use IsAuthenticated for POST requests and AllowAny for GET


  1. In Django ORM, you want to update multiple fields of an object in a single query. Which method should you use?
    a) save()
    b) update()
    c) bulk_update()
    d) create()

Answer: b) update()


  1. You want to handle different HTTP methods (e.g., GET, POST) for the same URL in a Django Rest Framework view. How can you differentiate between these methods in a APIView?
    a) Override perform_action()
    b) Use action_map
    c) Define separate views for each method
    d) Use get(), post(), etc., methods

Answer: d) Use get(), post(), etc., methods


  1. Your Django Rest Framework API is facing performance issues when dealing with a large number of queries in related models. How can you optimize this?
    a) Use annotate() to reduce queries
    b) Use select_related() for foreign key relations
    c) Use distinct() to reduce duplicate queries
    d) Use prefetch_related() for filtering

Answer: b) Use select_related() for foreign key relations

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