mirror of
				https://github.com/graphql-python/graphene-django.git
				synced 2025-11-04 09:57:53 +03:00 
			
		
		
		
	Fix DjangoModelFormMutation (#915)
* Fix DjangoModelFormMutation * Try and fix tests * Remove unused form
This commit is contained in:
		
							parent
							
								
									9d9a14c36d
								
							
						
					
					
						commit
						481d3ff35d
					
				| 
						 | 
				
			
			@ -162,6 +162,17 @@ class DjangoModelFormMutation(BaseDjangoFormMutation):
 | 
			
		|||
            _meta=_meta, input_fields=input_fields, **options
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def mutate_and_get_payload(cls, root, info, **input):
 | 
			
		||||
        form = cls.get_form(root, info, **input)
 | 
			
		||||
 | 
			
		||||
        if form.is_valid():
 | 
			
		||||
            return cls.perform_mutate(form, info)
 | 
			
		||||
        else:
 | 
			
		||||
            errors = ErrorType.from_errors(form.errors)
 | 
			
		||||
 | 
			
		||||
            return cls(errors=errors)
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def perform_mutate(cls, form, info):
 | 
			
		||||
        obj = form.save()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -29,6 +29,12 @@ class PetForm(forms.ModelForm):
 | 
			
		|||
        model = Pet
 | 
			
		||||
        fields = "__all__"
 | 
			
		||||
 | 
			
		||||
    def clean_age(self):
 | 
			
		||||
        age = self.cleaned_data["age"]
 | 
			
		||||
        if age >= 99:
 | 
			
		||||
            raise ValidationError("Too old")
 | 
			
		||||
        return age
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class PetType(DjangoObjectType):
 | 
			
		||||
    class Meta:
 | 
			
		||||
| 
						 | 
				
			
			@ -243,6 +249,10 @@ class ModelFormMutationTests(TestCase):
 | 
			
		|||
                        name
 | 
			
		||||
                        age
 | 
			
		||||
                    }
 | 
			
		||||
                    errors {
 | 
			
		||||
                        field
 | 
			
		||||
                        messages
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            """
 | 
			
		||||
| 
						 | 
				
			
			@ -255,6 +265,42 @@ class ModelFormMutationTests(TestCase):
 | 
			
		|||
        self.assertEqual(pet.name, "Mia")
 | 
			
		||||
        self.assertEqual(pet.age, 10)
 | 
			
		||||
 | 
			
		||||
    def test_model_form_mutation_invalid_input(self):
 | 
			
		||||
        class PetMutation(DjangoModelFormMutation):
 | 
			
		||||
            pet = Field(PetType)
 | 
			
		||||
 | 
			
		||||
            class Meta:
 | 
			
		||||
                form_class = PetForm
 | 
			
		||||
 | 
			
		||||
        class Mutation(ObjectType):
 | 
			
		||||
            pet_mutation = PetMutation.Field()
 | 
			
		||||
 | 
			
		||||
        schema = Schema(query=MockQuery, mutation=Mutation)
 | 
			
		||||
 | 
			
		||||
        result = schema.execute(
 | 
			
		||||
            """ mutation PetMutation {
 | 
			
		||||
                petMutation(input: { name: "Mia", age: 99 }) {
 | 
			
		||||
                    pet {
 | 
			
		||||
                        name
 | 
			
		||||
                        age
 | 
			
		||||
                    }
 | 
			
		||||
                    errors {
 | 
			
		||||
                        field
 | 
			
		||||
                        messages
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            """
 | 
			
		||||
        )
 | 
			
		||||
        self.assertIs(result.errors, None)
 | 
			
		||||
        self.assertEqual(result.data["petMutation"]["pet"], None)
 | 
			
		||||
        self.assertEqual(
 | 
			
		||||
            result.data["petMutation"]["errors"],
 | 
			
		||||
            [{"field": "age", "messages": ["Too old"],}],
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        self.assertEqual(Pet.objects.count(), 0)
 | 
			
		||||
 | 
			
		||||
    def test_model_form_mutation_mutate_invalid_form(self):
 | 
			
		||||
        class PetMutation(DjangoModelFormMutation):
 | 
			
		||||
            class Meta:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user