TCS Wings 1 T4 Django - Set 2
- You need to implement JWT token-based authentication for an API where the token should expire after 1 hour. Which setting should you modify?
a)ACCESS_TOKEN_LIFETIMEin the Simple JWT configuration
b)JWT_SECRET_KEY
c)REFRESH_TOKEN_LIFETIME
d)SESSION_EXPIRATION_TIME
Answer: a) ACCESS_TOKEN_LIFETIME in the Simple JWT configuration
- In your Django application, a
Bookmodel has a foreign key to anAuthormodel. How do you fetch all books written by a particular author using Django ORM?
a)Book.objects.get(author=author)
b)Book.objects.filter(author=author)
c)Author.objects.filter(book__title=title)
d)Book.objects.filter(author__name=author)
Answer: b) Book.objects.filter(author=author)
- In Django Rest Framework, you need to handle a
POSTrequest to create a newOrder. How do you handle the validation of the request data before saving it to the database?
a) Useform_valid()method
b) Override thecreate()method in the serializer
c) Use aSerializerMethodFieldin the serializer
d) Use aManyToManyFieldin the model
Answer: b) Override the create() method in the serializer
- You are developing a Django application that needs to send emails to users when they register. What Django feature would you use to configure and send these emails?
a)Django Middleware
b)EmailMessageclass fromdjango.core.mail
c)Django Sessions
d)Django Caching
Answer: b) EmailMessage class from django.core.mail
- You are working on a Django Rest Framework API that lists a large dataset. To reduce the load on the database, you decide to use query optimization. Which of the following methods would improve performance by reducing the number of database queries?
a) Usefilter()
b) Useselect_related()
c) Usecount()
d) Useorder_by()
Answer: b) Use select_related()
- Your Django model contains sensitive user data, and you need to restrict access based on user roles. What is the best way to enforce role-based permissions in Django Rest Framework?
a) UseIsAuthenticatedpermission
b) Use custom permissions in DRF
c) Define user roles in theurls.pyfile
d) Implement@admin_requireddecorator
Answer: b) Use custom permissions in DRF
- You are building a Django application that allows users to reset their passwords. Which built-in Django view would you use to handle the password reset process?
a)PasswordChangeView
b)PasswordResetConfirmView
c)PasswordResetView
d)ResetView
Answer: c) PasswordResetView
- In a Django Rest Framework project, you need to create a serializer that includes a nested relationship between models. What field type should you use to handle nested objects in the serializer?
a)PrimaryKeyRelatedField
b)ManyToManyField
c)SerializerMethodField
d)NestedSerializer
Answer: a) PrimaryKeyRelatedField
- You are creating a Django model for a product catalog, and some products are only available in specific countries. How do you define a field that allows the selection of multiple countries for a product?
a) Use aForeignKeyfield
b) Use aCharFieldwithchoices
c) Use aManyToManyFieldto aCountrymodel
d) Use aOneToOneFieldwith aCountrymodel
Answer: c) Use a ManyToManyField to a Country model
- In Django, you need to apply a function to every HTTP request and response in your application for logging purposes. Which feature would you use to implement this globally?
a) Django Middleware
b) Django Views
c) Django Forms
d) Django URLs
Answer: a) Django Middleware

Comments
Post a Comment