From 2152f123cbd534413d5790ac449a6571963753ed Mon Sep 17 00:00:00 2001 From: Jonathan Weth Date: Wed, 17 Apr 2024 18:30:27 +0200 Subject: [PATCH] Add before_create_obj hook to BatchCreateMutation --- graphene_django_cud/mutations/core.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/graphene_django_cud/mutations/core.py b/graphene_django_cud/mutations/core.py index c27444f..1178dfb 100644 --- a/graphene_django_cud/mutations/core.py +++ b/graphene_django_cud/mutations/core.py @@ -378,7 +378,11 @@ def create_obj( model_field_values[name + "_id"] = obj_id # Foreign keys are added, we are ready to create our object - obj = Model.objects.create(**model_field_values) + obj = Model(**model_field_values) + + cls.before_create_obj(info, input, obj) + + obj.save() # Handle one to one rels if len(one_to_one_rels) > 0: @@ -408,8 +412,6 @@ def create_obj( setattr(obj, name, new_value) - obj.save() - # Handle extras fields for name, extras in many_to_many_extras.items(): field = Model._meta.get_field(name) @@ -757,6 +759,10 @@ def before_save(cls, root, info, *args, **kwargs): def after_mutate(cls, root, info, *args, **kwargs): return None + @classmethod + def before_create_obj(cls, info, input, obj): + return None + @classmethod def resolve_id(cls, id): return disambiguate_id(id)