TCS Wings 1 T4 Django - Set 4
- You are designing a multi-tenant Django application where each user should only have access to their own data. How do you filter queryset results to ensure this security?
a) Implement filtering in the templates
b) Useget_object_or_404()with user data
c) Override theget_queryset()method to include user-specific filtering
d) Useget_context_data()in class-based views
Answer: c) Override the get_queryset() method to include user-specific filtering
- In your Django application, users can belong to multiple groups. How do you represent this relationship in your model?
a) Use aOneToOneField
b) Use aForeignKey
c) Use aManyToManyField
d) Use aCharFieldwithchoices
Answer: c) Use a ManyToManyField
- You want to add pagination to a Django Rest Framework API to display 20 items per page. How do you configure this?
a) SetPAGINATE_BYin the view
b) SetPAGINATE_BY_PARAMin the view
c) ConfigurePAGE_SIZEin settings
d) Usemax_limitin the view
Answer: c) Configure PAGE_SIZE in settings
- In your Django Rest Framework project, you want to filter queryset results dynamically based on query parameters passed in the URL. Which method would allow you to achieve this?
a)filter_queryset()
b)get()
c)get_queryset()
d)get_context_data()
Answer: a) filter_queryset()
- You are developing a Django application where users can update their profiles. However, only certain fields like
first_nameandlast_namecan be updated. How do you restrict the update operation to specific fields?
a) Define allowed fields in theviews.py
b) Use thepartial=Trueargument in the serializer
c) Useread_only=Truein the model definition
d) Overridesave()method in the view
Answer: b) Use the partial=True argument in the serializer
- In your Django Rest Framework API, users must be authenticated via JWT to create, update, or delete resources. Which DRF setting should be used to enforce JWT authentication globally for all views?
a)DEFAULT_PERMISSION_CLASSES
b)DEFAULT_AUTHENTICATION_CLASSES
c)DEFAULT_THROTTLE_CLASSES
d)DEFAULT_RENDERER_CLASSES
Answer: b) DEFAULT_AUTHENTICATION_CLASSES
- You need to cache expensive database queries to optimize performance in your Django application. Which caching backend would you use to cache the query results?
a) Django Sessions
b) Django Middleware
c) Redis or Memcached
d) Django Templates
Answer: c) Redis or Memcached
- Your Django project requires a view that returns data in JSON format for an API. Which view class would you use to automatically convert the response to JSON?
a)TemplateView
b)DetailView
c)JsonResponse
d)RedirectView
Answer: c) JsonResponse
- You want to add a custom method to a Django Rest Framework serializer to include additional data in the response that is not part of the model. Which serializer field would you use for this?
a)CharField
b)BooleanField
c)SerializerMethodField
d)ModelSerializer
Answer: c) SerializerMethodField
- Your Django project has a
Categorymodel with a foreign key toProduct. How do you retrieve all categories that have no products assigned to them using Django ORM?
a)Category.objects.filter(product=None)
b)Category.objects.exclude(product__isnull=False)
c)Category.objects.filter(product__isnull=True)
d)Category.objects.filter(product__count=0)
Answer: c) Category.objects.filter(product__isnull=True)

Comments
Post a Comment