From 89ae9ecd53feabed3735c897f9be2e2ba72a3707 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 | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/graphene_django_cud/mutations/core.py b/graphene_django_cud/mutations/core.py index c27444f..128a6d0 100644 --- a/graphene_django_cud/mutations/core.py +++ b/graphene_django_cud/mutations/core.py @@ -252,6 +252,10 @@ def upsert_obj( Model, ) + @classmethod + def before_create_obj(cls, info, input, obj): + return None + @classmethod def create_obj( cls, @@ -378,7 +382,7 @@ 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) # Handle one to one rels if len(one_to_one_rels) > 0: @@ -402,13 +406,16 @@ def create_obj( else: extra_data = one_to_one_extras.get(name, {}) - # This is a nested field we need to take care of. + # This is a nested fselfield we need to take care of. value[field.field.name] = obj.pk new_value = cls.create_or_update_one_to_one_relation(obj, field, value, extra_data, info) setattr(obj, name, new_value) - obj.save() + + cls.before_create_obj(info, input, obj) + + obj.save() # Handle extras fields for name, extras in many_to_many_extras.items():