Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When and where to set some field value before mutation? #24

Closed
Rainshaw opened this issue Apr 25, 2021 · 6 comments
Closed

When and where to set some field value before mutation? #24

Rainshaw opened this issue Apr 25, 2021 · 6 comments

Comments

@Rainshaw
Copy link
Contributor

Hello, I have a model that has a "owner" field with it.

I have two questions:

  1. how can i make this field not required or even hidden on graphql api?
  2. how can i set it to the request user before save? or raise error when the both do not match if Q1 can not be solved.

Looking forward to your reply.

@bellini666
Copy link
Member

Hey @RainshawGao

  1. It depends. If you are talking regarding the type itself, take a look at the graphene-django documentation. If it is at the mutation, you can use exclude_fields to exclude some fields or only_fields to define a list of the fields that will be exposed (obs. graphene-django works very alike for the query, except that they use exclude and fields if I'm not mistaken)

  2. There's a lot of ways. You can use the before_save hook at the mutation to do that. You can override the clean_input method (https://github.com/0soft/graphene-django-plus/blob/master/graphene_django_plus/mutations.py#L607) and add the user yourself (call super() and then add the user by hand), etc

@Rainshaw
Copy link
Contributor Author

nice to hear from you!! Thanks for your reply 😊

as in my scenery, the owner field has to be exposed so admins could create the instance for other user, and this field is not null so it is marked required, I have tried a lot of ways to mark it not required but they all not work 😔

so I finally use the second way, to verify the request user and the owner field value in clean_input method and raise error.

Thank you again~

@bellini666
Copy link
Member

To automatically mark it as not required you need to mark it with blank=True and have a default value on it (both on the django field)

You can also define a list of required_fields at the mutation, then the mutation will only consider those as required and force everything else to not required.

@Rainshaw
Copy link
Contributor Author

yes, I had tried to define the required_fields but I get validationerror in clean_instance() func.
If I mark the field as blank=True or with default value, it works.
I have to keep the field as it is for it also affects the django admin page, and the model must have the owner field. if I change the field, there is too many code needing to change. (On the other hand, I also do not have permission to modify the model in my compony....T^T)

@bellini666
Copy link
Member

Hey @RainshawGao

Is the issue solved? Asking so I can close it here.

@Rainshaw
Copy link
Contributor Author

yes! I just make it using clean_input, clean_instance and before_save func

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants