from rest_framework import serializers
from django.core.validators import MinValueValidator
class EmployeeSerializer(serializers.Serializer):
name = serializers.CharField(max_length=100)
salary = serializers.DecimalField(max_digits=10, decimal_places=2, validators=[MinValueValidator(0)])
department = serializers.CharField(max_length=50)
email = serializers.EmailField()
def validate_salary(self, value):
if value > 1000000:
raise serializers.ValidationError("Salary cannot exceed 1000000")
return value
def validate(self, data):
if data['department'] == 'Management' and data['salary'] < 50000:
raise serializers.ValidationError("Management employees must earn at least 50000")
return dataCustom Validation in DRF Serializers
A salary of 999999 will pass field validation but fail object validation if department is 'Management' with salary below 50000. A salary of -100 will fail MinValueValidator.
The validate_salary method will be called before the MinValueValidator, allowing it to accept negative salaries if they're below 1000000.
The object-level validate method cannot access individual field values like data['salary'] directly.
EmailField does not perform automatic validation; you must add a custom validator to check email format.
Show answer & explanationAnswer
Correct answer
A salary of 999999 will pass field validation but fail object validation if department is 'Management' with salary below 50000. A salary of -100 will fail MinValueValidator.
Explanation
Field-level validation (validate_salary) checks individual fields, while object-level validation (validate) checks cross-field constraints. Validators are applied before custom validation methods.
All 29 Django REST API Development questions
- 1.What is the primary purpose of Django REST Framework (DRF)?
- 2.What is a Serializer in Django REST Framework?
- 3.Which decorator is used to convert a regular Django view into an API view in DRF?
- 4.What is the purpose of ViewSets in Django REST Framework?
- 5.Which class should be inherited to create a basic API view in DRF?
- 6.What is the default authentication method in Django REST Framework?
- 7.How do you register URLs for ViewSets in Django REST Framework?
- 8.What does the status module in DRF provide?
- 9.Which permission class allows only authenticated users to access an API endpoint?
- 10.What is the Response class used for in Django REST Framework?
- 11.Scenario:
- 12.What will be the output of the following code snippet?
- 13.Scenario:
- 14.What is the difference between ModelSerializer and Serializer in Django REST Framework?
- 15.Identify the bug in the following code snippet:
- 16.How do you implement pagination in a Django REST Framework API?
- 17.Scenario:
- 18.What is the purpose of the queryset attribute in a ViewSet?
- 19.Scenario:
- 20.What will be the output of this code snippet?
- 21.Understanding DRF Serializers and Nested Relationships
- 22.DRF Viewsets and Router Configuration Scenario
- 23.Authentication and Permissions in DRF
- 24.Pagination and Filtering in DRF Scenario
- 25.Custom Validation in DRF Serializers
- 26.Status Codes and Exception Handling in DRF
- 27.Throttling and Rate Limiting Scenario
- 28.Request and Response Cycle with Middleware
- 29.Advanced Serializer Optimization and Performance
