TCS Wings 1 T4 Django - Set 1
- You are developing a Django application where users need to create accounts and login. After logging in, they should be redirected to their dashboard. Which Django feature will handle the redirection after login?
a)RedirectView
b)LoginView
c)@login_required
d)LOGIN_REDIRECT_URL
Answer: d) LOGIN_REDIRECT_URL
- You have an e-commerce site built with Django, and users frequently submit reviews. You want to ensure that each user can only submit one review per product. How would you enforce this in the model?
a)unique=Trueon the review field
b) Add aUniqueConstraintcombininguserandproductfields
c) Use aForeignKeywithunique=True
d) Validate it in theforms.pyfile
Answer: b) Add a UniqueConstraint combining user and product fields
- Your Django Rest Framework API needs to return data for both admin and regular users, but you need to filter sensitive data for regular users. How would you customize the serializer for this?
a) UseSerializerMethodFieldto control the output
b) Override the__str__method
c) Useget_serializer_class()in views
d) Define a customsave()method in the model
Answer: c) Use get_serializer_class() in views
- You are creating a Django application that allows users to upload profile pictures. You want to restrict the file size to a maximum of 5 MB. How do you enforce this in Django?
a) Use aFileFieldwithmax_sizeargument
b) Use a custom validator in the model
c) Set theFileField'supload_toargument
d) Limit it in the template file
Answer: b) Use a custom validator in the model
- Your Django application allows users to create blog posts. To ensure the post title remains unique across all users, how would you enforce this constraint in the database?
a) Add a unique constraint on thetitlefield
b) UseCharFieldwithmax_length=200
c) UseTextFieldfor the title
d) Addnull=Falsein the field definition
Answer: a) Add a unique constraint on the title field
- You are using Django Rest Framework for an API that lists blog posts. The list should be paginated to show 10 items per page. Which setting would achieve this?
a)PAGE_SIZE = 10
b)REST_FRAMEWORK['DEFAULT_PAGINATION_CLASS']
c)DEFAULT_FILTER_BACKENDS
d)MAX_PAGE_SIZE = 10
Answer: b) REST_FRAMEWORK['DEFAULT_PAGINATION_CLASS']
- In your Django project, you want to execute custom logic every time a user model is saved. How do you achieve this?
a) Override thesave()method in the model
b) Use a pre-save signal
c) Use a post-save signal
d) Override the__str__()method
Answer: c) Use a post-save signal
- You need to store user passwords in a Django application securely. Which of the following options is best for password storage?
a) Store the password as plain text
b) Use thehashliblibrary for hashing passwords
c) Use Django's built-inUsermodel withmake_password()
d) Store passwords usingAESencryption
Answer: c) Use Django's built-in User model with make_password()
- You are building an API with Django Rest Framework where users need to be authenticated with JWT tokens. Which of the following libraries would you use to implement JWT authentication?
a)djangorestframework-simplejwt
b)OAuthlib
c)Django-Token-Auth
d)Django-Session-Auth
Answer: a) djangorestframework-simplejwt
- In your Django Rest Framework project, you have a
Productmodel that is frequently updated by multiple users. You want to return a 409 Conflict error when two users attempt to update the same product simultaneously. How would you achieve this?
a) Use Django’sF()expressions
b) Implement optimistic locking in the serializer
c) Useupdate_or_create()method
d) Use thesave()method directly in the view
Answer: b) Implement optimistic locking in the serializer

Comments
Post a Comment