TCS Wings 1 T4 Django - Set 8
- 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()
- 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 theurls.pyfile
c) In the viewset using@actiondecorator
d) In the model
Answer: c) In the viewset using @action decorator
- 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
- 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 withHttpOnlyflag
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
- 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) UseContentNegotiationclass
b) Overrideget_serializer()method
c) SetDEFAULT_RENDERER_CLASSESinsettings.py
d) Useaccept()header in views
Answer: c) Set DEFAULT_RENDERER_CLASSES in settings.py
- You are building a Django application that uses multiple databases. How do you configure Django to route queries to different databases?
a) UseDATABASESsetting insettings.py
b) Usedb_for_readanddb_for_writein the model
c) Define separate models for each database
d) Use Django'srouter.py
Answer: a) Use DATABASES setting in settings.py
- 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) UseAllowAnypermission
b) UseIsAuthenticatedfor POST requests andAllowAnyfor GET
c) UseIsAuthenticatedfor all requests
d) Override theperform_create()method
Answer: b) Use IsAuthenticated for POST requests and AllowAny for GET
- 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()
- 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) Overrideperform_action()
b) Useaction_map
c) Define separate views for each method
d) Useget(),post(), etc., methods
Answer: d) Use get(), post(), etc., methods
- 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) Useannotate()to reduce queries
b) Useselect_related()for foreign key relations
c) Usedistinct()to reduce duplicate queries
d) Useprefetch_related()for filtering
Answer: b) Use select_related() for foreign key relations

Comments
Post a Comment