Posts

Showing posts from October, 2024

TCS Wings 1 T4 Django - Set 10

Image
You are building a multi-tenant Django application where each user can only access their own data. How do you restrict access in Django Rest Framework? a) Use permissions.IsAuthenticated b) Override get_queryset() method in the view c) Use AllowAny permission d) Filter by user in the serializer Answer: b) Override get_queryset() method in the view In your Django project, you want to schedule periodic tasks such as clearing expired sessions. Which library should you use to schedule these tasks? a) Django Signals b) Django Celery c) Django Cache d) Django Middleware Answer: b) Django Celery You need to retrieve the first object from a Django ORM queryset, or return None if no objects are found. Which method should you use? a) get() b) first() c) last() d) filter() Answer: b) first() In your Django Rest Framework project, you want to prevent users from making too many API requests within a short period. Which feature would you implement? a) Permission Classes b) Rate Limiting c) Aut...

TCS Wings 1 T4 Django - Set 9

Image
You are building a Django Rest Framework API that allows users to upload files. You want to set a limit on the file types that can be uploaded. Where should you enforce this restriction? a) In the views.py file b) In the models.py file c) In the serializers.py file using a custom validator d) In the settings.py file Answer: c) In the serializers.py file using a custom validator You want to run specific actions after an instance is saved in Django ORM, such as sending a notification. Which method should you override in the model to achieve this? a) pre_save() b) post_save() c) save() d) clean() Answer: c) save() In a Django Rest Framework project, you need to validate input data before saving it to the database. Which method should you override in your serializer class? a) save() b) validate() c) is_valid() d) create() Answer: b) validate() You are building a Django project that uses a large number of related models. To improve query performance when accessing related objects, wh...

TCS Wings 1 T4 Django - Set 8

Image
In Django ORM, how can you prefetch related objects to optimize database queries for many-to-many relationships? a) select_related() b) annotate() c) values() d) prefetch_related() Answer: d) prefetch_related() You want to define custom actions for a Django Rest Framework ModelViewSet . Where should you define these custom actions? a) In the serializer b) In the urls.py file c) In the viewset using @action decorator d) In the model Answer: c) In the viewset using @action decorator Your Django project needs to send a confirmation email to users after they register. Which Django feature would you use to send emails? a) Django Celery b) Django Signals c) Django Email Backend d) Django Tasks Answer: c) Django Email Backend You are implementing a JWT-based authentication system for your API. How would you securely store the JWT token on the client-side? a) Store it in local storage b) Store it in a session cookie with HttpOnly flag c) Include it in the URL parameters d) Store it in a ...

TCS Wings 1 T4 Django - Set 7

Image
You are developing an API in Django Rest Framework where you want to return paginated results for a list of items. How can you add pagination globally to your API views? a) Set PAGINATE_BY in the view b) Add pagination settings in settings.py c) Use PageNumberPagination in each view d) Define a custom pagination class Answer: b) Add pagination settings in settings.py You want to query the last 10 records of a model in Django ORM, sorted by creation date. Which method would you use? a) Model.objects.filter().limit(10) b) Model.objects.all()[:10] c) Model.objects.order_by('-created_at')[:10] d) Model.objects.last(10) Answer: c) Model.objects.order_by('-created_at')[:10] In your Django project, you want to implement caching for API responses to improve performance. Which middleware should you use to add caching? a) SessionMiddleware b) CacheMiddleware c) SecurityMiddleware d) CorsMiddleware Answer: b) CacheMiddleware In Django ORM, how do you retrieve only distinct va...

TCS Wings 1 T4 Django - Set 6

Image
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 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() 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 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 excl...

TCS Wings 1 T4 Django - Set 5

Image
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 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() , and delete() ? a) Use IsAuthenticated globally b) Set permissions in the urls.py c) Use get_permissions() to define different permissions f...

TCS Wings 1 T4 Django - Set 4

Image
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 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 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 se...

TCS Wings 1 T4 Django - Set 3

Image
You are developing a Django application that requires users to be authenticated to access specific pages. How do you enforce authentication for a particular view? a) Use the @login_required decorator b) Override the save() method in the view c) Use a custom form validation method d) Implement session management in urls.py Answer: a) Use the @login_required decorator Your Django application uses JWT tokens for authentication. You need to allow users to refresh their JWT tokens after they expire. Which token type is used for this purpose? a) Access token b) Refresh token c) ID token d) Authorization token Answer: b) Refresh token You are working on a Django Rest Framework API where a user can upload a file. You want to restrict the file size and type. How can you enforce this in your API? a) Override the create() method in the view b) Implement validation in the serializer c) Restrict it in the model definition d) Handle it in the frontend JavaScript Answer: b) Implement validation...

TCS Wings 1 T4 Django - Set 2

Image
  You need to implement JWT token-based authentication for an API where the token should expire after 1 hour. Which setting should you modify? a) ACCESS_TOKEN_LIFETIME in the Simple JWT configuration b) JWT_SECRET_KEY c) REFRESH_TOKEN_LIFETIME d) SESSION_EXPIRATION_TIME Answer: a) ACCESS_TOKEN_LIFETIME in the Simple JWT configuration In your Django application, a Book model has a foreign key to an Author model. How do you fetch all books written by a particular author using Django ORM? a) Book.objects.get(author=author) b) Book.objects.filter(author=author) c) Author.objects.filter(book__title=title) d) Book.objects.filter(author__name=author) Answer: b) Book.objects.filter(author=author) In Django Rest Framework, you need to handle a POST request to create a new Order . How do you handle the validation of the request data before saving it to the database? a) Use form_valid() method b) Override the create() method in the serializer c) Use a SerializerMethodField in the ser...

TCS Wings 1 T4 Django - Set 1

Image
You are developing a Django application where users need to create accounts and login. After logging in, they should be redirected to their dashboard. Which Django feature will handle the redirection after login? a) RedirectView b) LoginView c) @login_required d) LOGIN_REDIRECT_URL Answer: d) LOGIN_REDIRECT_URL You have an e-commerce site built with Django, and users frequently submit reviews. You want to ensure that each user can only submit one review per product. How would you enforce this in the model? a) unique=True on the review field b) Add a UniqueConstraint combining user and product fields c) Use a ForeignKey with unique=True d) Validate it in the forms.py file Answer: b) Add a UniqueConstraint combining user and product fields Your Django Rest Framework API needs to return data for both admin and regular users, but you need to filter sensitive data for regular users. How would you customize the serializer for this? a) Use SerializerMethodField to control the output ...