TCS Wings 1 T4 Django - Set 4



  1. 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) Use get_object_or_404() with user data
    c) Override the get_queryset() method to include user-specific filtering
    d) Use get_context_data() in class-based views

Answer: c) Override the get_queryset() method to include user-specific filtering


  1. In your Django application, users can belong to multiple groups. How do you represent this relationship in your model?
    a) Use a OneToOneField
    b) Use a ForeignKey
    c) Use a ManyToManyField
    d) Use a CharField with choices

Answer: c) Use a ManyToManyField


  1. You want to add pagination to a Django Rest Framework API to display 20 items per page. How do you configure this?
    a) Set PAGINATE_BY in the view
    b) Set PAGINATE_BY_PARAM in the view
    c) Configure PAGE_SIZE in settings
    d) Use max_limit in the view

Answer: c) Configure PAGE_SIZE in settings


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


  1. You are developing a Django application where users can update their profiles. However, only certain fields like first_name and last_name can be updated. How do you restrict the update operation to specific fields?
    a) Define allowed fields in the views.py
    b) Use the partial=True argument in the serializer
    c) Use read_only=True in the model definition
    d) Override save() method in the view

Answer: b) Use the partial=True argument in the serializer


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


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


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


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


  1. Your Django project has a Category model with a foreign key to Product. 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

Popular posts from this blog

TCS Wings 1 T4 Django - Set 1

TCS Wings 1 T4 Django - Set 10