strawberry-django-plus icon indicating copy to clipboard operation
strawberry-django-plus copied to clipboard

Extending build in create mutations when only null=True

Open nour-bouzid opened this issue 1 year ago • 5 comments

I have the following a model with the following field:

class Invitation(models.Model):
  ...
  department = models.ForeignKey(
          Department,
          null=True,
          on_delete=models.CASCADE,
          related_name="invitee_department",
      )

And I am trying to write a mutation for creating invitations:

@gql.django.mutation
    def create_invitation(
        self, info, input: InvitationInput
    ) -> InvitationType:
           data = vars(input)
           ...
           # for simplicity
           department = None
           data.update(
                {
                    "department": department,
                    ....
                },
            )

            invite = resolvers.create(
                info, Invitation, resolvers.parse_input(info, data)
            )
            ...
            return (cast(CustomInvitationType, invite),)

the create resolver gives the following message:

{
          "field": "department",
          "kind": "VALIDATION",
          "message": "This field cannot be blank."
        }

I was only able to solve this by adding blank=True in the field but I don't want to do this. Is there any way around this?

nour-bouzid avatar Apr 13 '23 15:04 nour-bouzid