-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add field name mappings functionality
This commit allows users to specify aliases for the automatically extracted field names of models, called "field_name_mappings". For instance, if a model has a field name "name", but you actually want the API the expose this field as "tag", you might supply a field name mapping as follows: class Test(DjangoCreateMutation): class Meta: field_name_mappings = { "name": "tag" } The mappings are always `db-name` => `api-name`. In addition, two extra flags are added to the meta attributes of most mutations: - `use_id_suffixes_for_fk` - `use_id_suffixes_for_m2m` Supplying these, respectively, will add field name mappings for all foreign keys and m2m fields (and also for the opposite side of fks) such that "_id" are appended to the foreign key names, and "_ids" to m2m names. For instance, if you have a model with an FK named "user", this field would by default be exposed as an ID field with the name "user". If you set `use_id_suffixes_for_fk=True`, the field would now be exposed in the API as `userId` (since snake_case is auto-converted to camelCase by graphene). Similarly for m2ms (or the other side of fks, m2os): Say you have a m2m relation with a field named `groups`, setting `use_id_suffixes_for_m2m` would make the input field be `groupsIds`. The double pluralization is intentional (we don't accept the responsibility of handling all the ways one might name a m2m field and converting it into proper pluralized English). You can override this explicitly using `field_name_mappings`. Finally, both of these settings can be set globally by using the following two django settings: - `GRAPHENE_DJANGO_CUD_USE_ID_SUFFIXES_FOR_FK` - `GRAPHENE_DJANGO_CUD_USE_ID_SUFFIXES_FOR_M2M`
- Loading branch information
Showing
12 changed files
with
1,117 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
from django.conf import settings | ||
|
||
OPERATION_UPDATE = "update" | ||
OPERATION_DELETE = "delete" | ||
OPERATION_CREATE = "create" | ||
OPERATION_UPSERT = "upsert" | ||
|
||
USE_ID_SUFFIXES_FOR_FK_SETTINGS_KEY = "GRAPHENE_DJANGO_CUD_USE_ID_SUFFIXES_FOR_FK" | ||
USE_ID_SUFFIXES_FOR_M2M_SETTINGS_KEY = "GRAPHENE_DJANGO_CUD_USE_ID_SUFFIXES_FOR_M2M" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.