TCS Wings 1 T4 Django - Set 6



  1. You need to create a Django Rest Framework API that restricts access to authenticated users only. Which permission class should you use to enforce this?
    a) IsAdminUser
    b) IsAuthenticated
    c) AllowAny
    d) IsAuthenticatedOrReadOnly

Answer: b) IsAuthenticated


  1. In your Django project, you want to retrieve objects in batches to improve performance for large datasets. Which Django ORM function should you use?
    a) all()
    b) filter()
    c) iterator()
    d) get()

Answer: c) iterator()


  1. You want to ensure that only users with an active status can log in to your Django application. How do you enforce this in Django Rest Framework?
    a) Override the login() view
    b) Use IsAuthenticated permission
    c) Override the authenticate() method
    d) Set a custom user model and check the is_active flag

Answer: d) Set a custom user model and check the is_active flag


  1. In Django ORM, how can you exclude specific objects from a query result?
    a) Use filter() with NOT condition
    b) Use exclude()
    c) Use annotate()
    d) Use exclude() with ManyToManyField

Answer: b) Use exclude()


  1. You want to throttle requests to your Django Rest Framework API to prevent abuse. Which class should you use to set request limits?
    a) ThrottleViewSet
    b) RateThrottle
    c) CustomThrottle
    d) RequestRateThrottle

Answer: b) RateThrottle


  1. You need to add a "created_at" timestamp to your Django models that automatically sets the current date and time when a record is created. Which field type should you use?
    a) DateField(auto_now=True)
    b) DateTimeField(auto_now_add=True)
    c) DateTimeField()
    d) DateField()

Answer: b) DateTimeField(auto_now_add=True)


  1. In your Django Rest Framework API, you want to give anonymous users read access but restrict write access to authenticated users. Which permission class should you use?
    a) IsAuthenticated
    b) AllowAny
    c) IsAdminUser
    d) IsAuthenticatedOrReadOnly

Answer: d) IsAuthenticatedOrReadOnly


  1. You want to write a custom Django management command that clears old user sessions from the database. In which directory should you place the custom command file?
    a) app/utils/commands/
    b) app/management/commands/
    c) app/migrations/commands/
    d) app/views/commands/

Answer: b) app/management/commands/


  1. In your Django ORM query, you want to get the total number of comments per blog post. Which function will allow you to count the related comments?
    a) aggregate(Count('comments'))
    b) annotate(Count('comments'))
    c) count()
    d) select_related('comments')

Answer: b) annotate(Count('comments'))


  1. Your Django application requires a custom user model to include additional fields such as phone_number. How do you define a custom user model in Django?
    a) Override the User model directly
    b) Subclass AbstractUser and define the new fields
    c) Modify the default user model in settings.py
    d) Use ForeignKey to add extra fields to the user

Answer: b) Subclass AbstractUser and define the new fields

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