TCS Wings 1 T4 Django - Set 1


  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


  1. 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=True on the review field
    b) Add a UniqueConstraint combining user and product fields
    c) Use a ForeignKey with unique=True
    d) Validate it in the forms.py file

Answer: b) Add a UniqueConstraint combining user and product fields


  1. 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) Use SerializerMethodField to control the output
    b) Override the __str__ method
    c) Use get_serializer_class() in views
    d) Define a custom save() method in the model

Answer: c) Use get_serializer_class() in views


  1. 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 a FileField with max_size argument
    b) Use a custom validator in the model
    c) Set the FileField's upload_to argument
    d) Limit it in the template file

Answer: b) Use a custom validator in the model


  1. 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 the title field
    b) Use CharField with max_length=200
    c) Use TextField for the title
    d) Add null=False in the field definition

Answer: a) Add a unique constraint on the title field


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


  1. In your Django project, you want to execute custom logic every time a user model is saved. How do you achieve this?
    a) Override the save() 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


  1. 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 the hashlib library for hashing passwords
    c) Use Django's built-in User model with make_password()
    d) Store passwords using AES encryption

Answer: c) Use Django's built-in User model with make_password()


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


  1. In your Django Rest Framework project, you have a Product model 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’s F() expressions
    b) Implement optimistic locking in the serializer
    c) Use update_or_create() method
    d) Use the save() method directly in the view

Answer: b) Implement optimistic locking in the serializer

Comments

Popular posts from this blog

TCS Wings 1 T4 Django - Set 10

TCS Wings 1 T4 Django - Set 4