Optional and required fields¶
This section is primarily relevant for create, update and patch mutations.
General rules¶
There are certain rules which decide whether or not a field is marked as required. For patch mutations, all fields are always marked as optional. For update and create mutations, however, the following rules apply:
- If the field has an explicit override, this is used.
- If the field has a default-value, it is marked as optional.
- If the field is a many-to-many field and has blank=True, it is marked as optional.
- If the field is nullable, it is marked as optional.
- In all other scenarios, the field is marked as required.
Explicitly overriding¶
A field can explicitly be marked as optional or required with the meta-attributes optional_fields
and required_fields
:
class CreateUserMutation(DjangoCreateMutation):
class Meta:
model = User
required_fields = ("first_name",)
optional_fields = ("last_name",)