TCS Wings 1 T4 Django - Set 5
- 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) UseIsAuthenticatedpermission class
b) Define custom permission classes
c) Add user role checks inurls.py
d) Use thelogin_requireddecorator
Answer: b) Define custom permission classes
- 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/
- 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(), anddelete()?
a) UseIsAuthenticatedglobally
b) Set permissions in theurls.py
c) Useget_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
- 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()
- 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 theAuthorizationheader 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
- 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()withAvg()
b)aggregate()withSum()
c)filter()withAvg()
d)annotate()withCount()
Answer: a) annotate() with Avg()
- 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
- 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 withread_only=True
b) Override thevalidate()method in the serializer
c) Define these fields asnull=Truein the model
d) Usepermissionsin the view
Answer: a) Define the non-editable fields in the serializer with read_only=True
- 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')
- 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) Useset_password()on the user object
b) Usesave()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
Post a Comment