mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-23 01:57:08 +03:00
Exclude id from mutation, useful for create operations.
This commit is contained in:
parent
dc561c68c4
commit
c1bd3c4c15
|
@ -161,6 +161,7 @@ class DjangoModelFormMutation(BaseDjangoFormMutation):
|
||||||
|
|
||||||
form = form_class()
|
form = form_class()
|
||||||
input_fields = fields_for_form(form, only_fields, exclude_fields)
|
input_fields = fields_for_form(form, only_fields, exclude_fields)
|
||||||
|
if "id" not in exclude_fields:
|
||||||
input_fields["id"] = graphene.ID()
|
input_fields["id"] = graphene.ID()
|
||||||
|
|
||||||
registry = get_global_registry()
|
registry = get_global_registry()
|
||||||
|
|
|
@ -13,7 +13,7 @@ class MyForm(forms.Form):
|
||||||
class PetForm(forms.ModelForm):
|
class PetForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Pet
|
model = Pet
|
||||||
fields = ("name",)
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
def test_needs_form_class():
|
def test_needs_form_class():
|
||||||
|
@ -51,6 +51,30 @@ class ModelFormMutationTests(TestCase):
|
||||||
self.assertEqual(PetMutation._meta.return_field_name, "pet")
|
self.assertEqual(PetMutation._meta.return_field_name, "pet")
|
||||||
self.assertIn("pet", PetMutation._meta.fields)
|
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):
|
def test_return_field_name_is_camelcased(self):
|
||||||
class PetMutation(DjangoModelFormMutation):
|
class PetMutation(DjangoModelFormMutation):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -76,9 +100,9 @@ class ModelFormMutationTests(TestCase):
|
||||||
class Meta:
|
class Meta:
|
||||||
form_class = PetForm
|
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)
|
self.assertEqual(Pet.objects.count(), 1)
|
||||||
pet.refresh_from_db()
|
pet.refresh_from_db()
|
||||||
|
@ -90,11 +114,12 @@ class ModelFormMutationTests(TestCase):
|
||||||
class Meta:
|
class Meta:
|
||||||
form_class = PetForm
|
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)
|
self.assertEqual(Pet.objects.count(), 1)
|
||||||
pet = Pet.objects.get()
|
pet = Pet.objects.get()
|
||||||
self.assertEqual(pet.name, "Mia")
|
self.assertEqual(pet.name, "Mia")
|
||||||
|
self.assertEqual(pet.age, 10)
|
||||||
self.assertEqual(result.errors, [])
|
self.assertEqual(result.errors, [])
|
||||||
|
|
||||||
def test_model_form_mutation_mutate_invalid_form(self):
|
def test_model_form_mutation_mutate_invalid_form(self):
|
||||||
|
@ -107,6 +132,8 @@ class ModelFormMutationTests(TestCase):
|
||||||
# A pet was not created
|
# A pet was not created
|
||||||
self.assertEqual(Pet.objects.count(), 0)
|
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].field, "name")
|
||||||
self.assertEqual(result.errors[0].messages, ["This field is required."])
|
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."])
|
||||||
|
|
|
@ -8,6 +8,7 @@ CHOICES = ((1, "this"), (2, _("that")))
|
||||||
|
|
||||||
class Pet(models.Model):
|
class Pet(models.Model):
|
||||||
name = models.CharField(max_length=30)
|
name = models.CharField(max_length=30)
|
||||||
|
age = models.PositiveIntegerField()
|
||||||
|
|
||||||
|
|
||||||
class FilmDetails(models.Model):
|
class FilmDetails(models.Model):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user