From c1bd3c4c1551c1d0230c696f4fb68ff5388debfa Mon Sep 17 00:00:00 2001 From: Waldecir Santos Date: Sun, 22 Jul 2018 23:34:01 +0100 Subject: [PATCH] Exclude id from mutation, useful for create operations. --- graphene_django/forms/mutation.py | 3 +- graphene_django/forms/tests/test_mutation.py | 37 +++++++++++++++++--- graphene_django/tests/models.py | 1 + 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/graphene_django/forms/mutation.py b/graphene_django/forms/mutation.py index 701bac8..63ea089 100644 --- a/graphene_django/forms/mutation.py +++ b/graphene_django/forms/mutation.py @@ -161,7 +161,8 @@ class DjangoModelFormMutation(BaseDjangoFormMutation): form = form_class() input_fields = fields_for_form(form, only_fields, exclude_fields) - input_fields["id"] = graphene.ID() + if "id" not in exclude_fields: + input_fields["id"] = graphene.ID() registry = get_global_registry() model_type = registry.get_type_for_model(model) diff --git a/graphene_django/forms/tests/test_mutation.py b/graphene_django/forms/tests/test_mutation.py index 0404010..b7d7d59 100644 --- a/graphene_django/forms/tests/test_mutation.py +++ b/graphene_django/forms/tests/test_mutation.py @@ -13,7 +13,7 @@ class MyForm(forms.Form): class PetForm(forms.ModelForm): class Meta: model = Pet - fields = ("name",) + fields = '__all__' def test_needs_form_class(): @@ -51,6 +51,30 @@ class ModelFormMutationTests(TestCase): self.assertEqual(PetMutation._meta.return_field_name, "pet") self.assertIn("pet", PetMutation._meta.fields) + def test_default_input_meta_fields(self): + class PetMutation(DjangoModelFormMutation): + class Meta: + form_class = PetForm + + self.assertEqual(PetMutation._meta.model, Pet) + self.assertEqual(PetMutation._meta.return_field_name, "pet") + self.assertIn("name", PetMutation.Input._meta.fields) + self.assertIn("client_mutation_id", PetMutation.Input._meta.fields) + self.assertIn("id", PetMutation.Input._meta.fields) + + def test_exclude_fields_input_meta_fields(self): + class PetMutation(DjangoModelFormMutation): + class Meta: + form_class = PetForm + exclude_fields = ['id'] + + self.assertEqual(PetMutation._meta.model, Pet) + self.assertEqual(PetMutation._meta.return_field_name, "pet") + self.assertIn("name", PetMutation.Input._meta.fields) + self.assertIn("age", PetMutation.Input._meta.fields) + self.assertIn("client_mutation_id", PetMutation.Input._meta.fields) + self.assertNotIn("id", PetMutation.Input._meta.fields) + def test_return_field_name_is_camelcased(self): class PetMutation(DjangoModelFormMutation): class Meta: @@ -76,9 +100,9 @@ class ModelFormMutationTests(TestCase): class Meta: form_class = PetForm - pet = Pet.objects.create(name="Axel") + pet = Pet.objects.create(name="Axel", age=10) - result = PetMutation.mutate_and_get_payload(None, None, id=pet.pk, name="Mia") + result = PetMutation.mutate_and_get_payload(None, None, id=pet.pk, name="Mia", age=10) self.assertEqual(Pet.objects.count(), 1) pet.refresh_from_db() @@ -90,11 +114,12 @@ class ModelFormMutationTests(TestCase): class Meta: form_class = PetForm - result = PetMutation.mutate_and_get_payload(None, None, name="Mia") + result = PetMutation.mutate_and_get_payload(None, None, name="Mia", age=10) self.assertEqual(Pet.objects.count(), 1) pet = Pet.objects.get() self.assertEqual(pet.name, "Mia") + self.assertEqual(pet.age, 10) self.assertEqual(result.errors, []) def test_model_form_mutation_mutate_invalid_form(self): @@ -107,6 +132,8 @@ class ModelFormMutationTests(TestCase): # A pet was not created self.assertEqual(Pet.objects.count(), 0) - self.assertEqual(len(result.errors), 1) + self.assertEqual(len(result.errors), 2) self.assertEqual(result.errors[0].field, "name") self.assertEqual(result.errors[0].messages, ["This field is required."]) + self.assertEqual(result.errors[1].field, "age") + self.assertEqual(result.errors[1].messages, ["This field is required."]) diff --git a/graphene_django/tests/models.py b/graphene_django/tests/models.py index 45ab737..4fe546d 100644 --- a/graphene_django/tests/models.py +++ b/graphene_django/tests/models.py @@ -8,6 +8,7 @@ CHOICES = ((1, "this"), (2, _("that"))) class Pet(models.Model): name = models.CharField(max_length=30) + age = models.PositiveIntegerField() class FilmDetails(models.Model):