💻
Bookstores
  • Tech Stacks
  • Django
    • Django REST framework
    • Serializer validation
    • Form validation in view class
  • Use Open API data model
  • Server Side react javascript
  • React context and redux difference
  • Migrate state management from Redux to Jotai
  • Migrate from REST to GraphQL
  • Migrate from REST to gRPC
  • How to handle cross origin?
Powered by GitBook
On this page

Was this helpful?

  1. Django

Serializer validation

  • Implement validate(self, data) method

    File : book/serializers.py

    class AuthorSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = Author
            fields = '__all__'
    
        def validate(self, data):
            """
            Check that start is before finish.
            """
            first_name = data["first_name"]
            last_name = data["last_name"]
    
            query = Author.objects.filter(
                first_name=first_name, last_name=last_name)
            authors = query.all()
    
            if len(authors) > 0:
                raise serializers.ValidationError("Author already exist")
            return data
  • If invalid raise error; otherwise, return data.

PreviousDjango REST frameworkNextForm validation in view class

Last updated 3 years ago

Was this helpful?