Skip to content

Commit

Permalink
remove try/catch when create/upadate obj
Browse files Browse the repository at this point in the history
  • Loading branch information
GiuseppeNovielli committed Sep 9, 2024
1 parent 86684ab commit 439f7d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 40 deletions.
48 changes: 9 additions & 39 deletions drf_fullclean/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,14 @@
https://github.com/encode/django-rest-framework/discussions/7850#discussioncomment-8380135
"""
from copy import deepcopy
from rest_framework import serializers

from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.db.models.fields.reverse_related import ManyToManyRel

from django.core.exceptions import ValidationError as DjangoValidationError, FieldDoesNotExist
from rest_framework import serializers

from django.forms.models import model_to_dict

from drf_fullclean.utils import remove_many_to_many

# DEBUG
def print_debug(message):
drf_full_clean = getattr(settings, 'DRF_FULL_CLEAN', {})
if not drf_full_clean.get('DEBUG', False):
return
print('\nDRF_FULL_CLEAN -> {}'.format(message))

def instance_to_dict(instance):
value_to_dict = None
try:
value_to_dict = model_to_dict(instance)
except:
try:
value_to_dict = instance.__dict__
except:
pass
return value_to_dict
from .utils import instance_to_dict, print_debug, remove_many_to_many


class FullCleanModelSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -76,10 +55,7 @@ def is_valid_model(self, raise_exception=False, exclude=None, validate_unique=Tr
obj = self.model_instance(self.Meta.model, self.get_validated_data(), self.get_instance_update_to_fullclean(), self.partial, extra_include, **kwargs)

print_debug('is_valid_model -> Instance To FullClean -> {} -- {}'.format(type(obj), instance_to_dict(obj)))
if obj:
errors = self.model_validation(obj, exclude, validate_unique, extra_include, **kwargs)
else:
raise Exception('Nested serializers are not supported.')
errors = self.model_validation(obj, exclude, validate_unique, extra_include, **kwargs)

if errors and raise_exception:
raise serializers.ValidationError(detail=errors)
Expand Down Expand Up @@ -135,10 +111,7 @@ def add_extra_include_to_instance(self, instance, extra_include, **kwargs):

# CREATE
def create_instance(self, model_class, validated_data, **kwargs):
try:
return model_class(**validated_data)
except Exception as e:
print_debug('Create Instance -> EXCEPTION -> {}'.format(e))
return model_class(**validated_data)


def model_instance_create(self, model_class, validated_data, extra_include=None, **kwargs):
Expand All @@ -152,14 +125,11 @@ def update_instance(self, instance, validated_data, partial=False, **kwargs):
if not instance:
return

try:
for field in instance._meta.fields:
if field.name not in validated_data:
continue
setattr(instance, field.name, validated_data[field.name])
return instance
except Exception as e:
print_debug('Update Instance -> EXCEPTION -> {}'.format(e))
for field in instance._meta.fields:
if field.name not in validated_data:
continue
setattr(instance, field.name, validated_data[field.name])
return instance



Expand Down
25 changes: 24 additions & 1 deletion drf_fullclean/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
from django.conf import settings
from django.forms.models import model_to_dict

def remove_many_to_many(serializer, validated_data, **kwargs):
for field in serializer.Meta.model._meta.many_to_many:
if field.name in validated_data:
del validated_data[field.name]
return validated_data
return validated_data


# DEBUG
def print_debug(message):
drf_full_clean = getattr(settings, 'DRF_FULL_CLEAN', {})
if not drf_full_clean.get('DEBUG', False):
return
print('\nDRF_FULL_CLEAN -> {}'.format(message))


def instance_to_dict(instance):
value_to_dict = None
try:
value_to_dict = model_to_dict(instance)
except:
try:
value_to_dict = instance.__dict__
except:
pass
return value_to_dict

0 comments on commit 439f7d3

Please sign in to comment.