TCS Wings 1 T4 Django - Set 5



  1. In your Django application, you need to restrict access to views based on the user’s role. How do you define role-based access control in Django Rest Framework?
    a) Use IsAuthenticated permission class
    b) Define custom permission classes
    c) Add user role checks in urls.py
    d) Use the login_required decorator

Answer: b) Define custom permission classes


  1. You are working on a Django project where you need to create a custom management command. Which directory structure is appropriate for defining custom management commands in a Django app?
    a) app/management/commands/
    b) app/utils/commands/
    c) app/views/commands/
    d) app/templates/commands/

Answer: a) app/management/commands/


  1. You have a Django Rest Framework viewset where different user roles have different access levels. How do you define permissions for different actions such as list(), create(), and delete()?
    a) Use IsAuthenticated globally
    b) Set permissions in the urls.py
    c) Use get_permissions() to define different permissions for each action
    d) Use Django Middleware to handle access

Answer: c) Use get_permissions() to define different permissions for each action


  1. You are building an API that allows users to update their account details, but you want to ensure that they can only update their own account. Which method would you override in Django Rest Framework to enforce this check?
    a) update()
    b) perform_update()
    c) partial_update()
    d) get_queryset()

Answer: b) perform_update()


  1. You are using JWT-based authentication in Django Rest Framework. After login, the user receives an access token. How do you ensure that the token is included in all future requests from the frontend?
    a) Store the token in the session
    b) Send the token in the Authorization header of every request
    c) Include the token in the request body
    d) Use cookies to store the token

Answer: b) Send the token in the Authorization header of every request


  1. In your Django ORM query, you need to calculate the average price of products in each category. Which ORM function would you use?
    a) annotate() with Avg()
    b) aggregate() with Sum()
    c) filter() with Avg()
    d) annotate() with Count()

Answer: a) annotate() with Avg()


  1. You are building a Django Rest Framework API where users can register and login. Upon login, you need to return both an access token and a refresh token. Which library can help you implement this?
    a) rest_framework_simplejwt
    b) django-rest-auth
    c) django-otp
    d) djangorestframework-recursive

Answer: a) rest_framework_simplejwt


  1. You want to prevent users from updating certain fields of a model in your Django Rest Framework API while allowing other fields to be editable. Which option best handles this scenario?
    a) Define the non-editable fields in the serializer with read_only=True
    b) Override the validate() method in the serializer
    c) Define these fields as null=True in the model
    d) Use permissions in the view

Answer: a) Define the non-editable fields in the serializer with read_only=True


  1. In Django ORM, how can you order query results by a field in descending order?
    a) Model.objects.order_by('-field_name')
    b) Model.objects.order_by('desc_field_name')
    c) Model.objects.filter('-field_name')
    d) Model.objects.sort('-field_name')

Answer: a) Model.objects.order_by('-field_name')


  1. You need to update the password for a user in your Django Rest Framework API, but you want to ensure that the password is hashed properly. How would you handle this?
    a) Use set_password() on the user object
    b) Use save() method without changes
    c) Encrypt the password manually in the view
    d) Store the password in plain text and then hash it later

Answer: a) Use set_password() on the user object

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