Skip to content

Call django model full_clean() when invoke serializer.is_valid() of ModelSerializer.

License

Notifications You must be signed in to change notification settings

giuseppenovielli/drf-fullclean

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

drf-fullclean

PyPI - Version PyPI - Downloads

Call django Model.full_clean(exclude=None, validate_unique=True) when invoke serializer.is_valid() of ModelSerializer

Django Rest Framework 3 Design

https://www.django-rest-framework.org/community/3.0-announcement/#differences-between-modelserializer-validation-and-modelform

Differences between ModelSerializer validation and ModelForm.

This change also means that we no longer use the .full_clean() method on model instances, but instead perform all validation explicitly on the serializer. This gives a cleaner separation, and ensures > that there's no automatic validation behavior on ModelSerializer classes that can't also be easily replicated on regular Serializer classes.

Discussions

encode/django-rest-framework#7850

Warning!

  1. One ModelSerializer -> Use this library.
  2. Multiple ModelSerializer -> PLEASE READ ME

Installation

pip install drf-fullclean

Configuration

Add the following code into settings.py

DRF_FULL_CLEAN = {
    "DEBUG" : False #set True if you want to see debug print
}

Usage

from drf_fullclean.serializers import FullCleanModelSerializer

class MyModelSerializerClass(FullCleanModelSerializer):
  class Meta:
      model = MyModel
      fields = '__all__
s = MyModelSerializerClass(data=request.POST)
s.is_valid(raise_exception=True)
s.save()

When you call s.is_valid(raise_exception=True) this method invoke also Model.full_clean() method.

The validation FAIL IF Model.full_clean() FAIL.

API

is_valid() is extended with Model.full_clean() api.

is_valid(self, raise_exception=False, exclude=None, validate_unique=True, extra_include=None, *args, **kwargs)

About

Call django model full_clean() when invoke serializer.is_valid() of ModelSerializer.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages