Skip to content

Django Model Cruddals

A base class to generating a GraphQL schema with CRUDDALS operations for a Django model using Cruddals.

This class is designed to be used as a base class for Django models. GraphQL types and resolvers are generated automatically based on the provided Django model.

To use this class, define a subclass and include a Meta class with the required attributes.

Example Usage:

from graphene_django_cruddals import DjangoModelCruddals
from your_django_app.models import YourModel

class YourModelCruddals(DjangoModelCruddals):
    class Meta:
        model = YourModel
        ... # Other options
from graphene_django_cruddals import DjangoModelCruddals
from your_django_app.models import YourModel

class YourModelCruddals(DjangoModelCruddals):
    class Meta:
        model = YourModel
        functions = ("create", "read", "update", "delete", "deactivate", "activate", "list", "search")
        # exclude_functions = ("create",)
        prefix = "New"
        suffix = "Suffix"
        cruddals_interfaces = (CustomCruddalsInterface,)
        # exclude_cruddals_interfaces = ("CustomCruddalsInterface",)
        # registry = your_registry
        field_for_activate_deactivate = "is_enabled"

Meta Class Options:

Note

These options can also be seen below in the definition of __init_subclass_with_meta__ method.

*model (DjangoModel):* The Django model class to be used for generating CRUDDALS operations.
*config (CruddalsBuilderConfig, optional):* Configuration object for customizing the behavior of the CRUDDALS operations. Defaults and recommended to None.
*functions (Tuple[FunctionType, ...], optional):* A tuple of functions ('create', 'read', 'update', 'delete', 'deactivate', 'activate', 'list', 'search') to be included in the generated GraphQL Schema. Defaults to None.
*exclude_functions (Tuple[FunctionType, ...], optional):* A tuple of functions to be excluded from the generated GraphQL Schema. Defaults to None.
*prefix (str, optional):* A prefix to be added to the generated GraphQL Types name. Defaults to "".
*suffix (str, optional):* A suffix to be added to the generated GraphQL Types name. Defaults to "".
*cruddals_interfaces (Tuple[Type[Any], ...], optional):* A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().
*exclude_cruddals_interfaces (Tuple[str, ...], optional):* A tuple of interface names to be excluded if `DjangoAppCruddals` or `DjangoProjectCruddals` is used. Defaults to ().
*registry (RegistryGlobal, optional):* The global registry object to be used for registering the generated GraphQL Schema. Defaults to None.
*field_for_activate_deactivate (str, optional):* The name of the field used for activating/deactivating the object. Defaults to "is_active".
Source code in graphene_django_cruddals/main.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
class DjangoModelCruddals(CruddalsModel):
    """
    A base class to generating a GraphQL schema with CRUDDALS operations for a **Django model** using Cruddals.

    This class is designed to be used as a base class for Django models.
    GraphQL types and resolvers are generated automatically based on the provided Django model.

    To use this class, define a subclass and include a `Meta` class with the required attributes.

    Example Usage:
    --------------


    === "Only Required Arguments"
        ```python
        from graphene_django_cruddals import DjangoModelCruddals
        from your_django_app.models import YourModel

        class YourModelCruddals(DjangoModelCruddals):
            class Meta:
                model = YourModel
                ... # Other options
        ```
    === "With Optional Arguments"
        ```python
        from graphene_django_cruddals import DjangoModelCruddals
        from your_django_app.models import YourModel

        class YourModelCruddals(DjangoModelCruddals):
            class Meta:
                model = YourModel
                functions = ("create", "read", "update", "delete", "deactivate", "activate", "list", "search")
                # exclude_functions = ("create",)
                prefix = "New"
                suffix = "Suffix"
                cruddals_interfaces = (CustomCruddalsInterface,)
                # exclude_cruddals_interfaces = ("CustomCruddalsInterface",)
                # registry = your_registry
                field_for_activate_deactivate = "is_enabled"
        ```

    Meta Class Options:
    -------------------

    Note:
        These options can also be seen below in the definition of `__init_subclass_with_meta__` method.

    ```markdown
    *model (DjangoModel):* The Django model class to be used for generating CRUDDALS operations.
    *config (CruddalsBuilderConfig, optional):* Configuration object for customizing the behavior of the CRUDDALS operations. Defaults and recommended to None.
    *functions (Tuple[FunctionType, ...], optional):* A tuple of functions ('create', 'read', 'update', 'delete', 'deactivate', 'activate', 'list', 'search') to be included in the generated GraphQL Schema. Defaults to None.
    *exclude_functions (Tuple[FunctionType, ...], optional):* A tuple of functions to be excluded from the generated GraphQL Schema. Defaults to None.
    *prefix (str, optional):* A prefix to be added to the generated GraphQL Types name. Defaults to "".
    *suffix (str, optional):* A suffix to be added to the generated GraphQL Types name. Defaults to "".
    *cruddals_interfaces (Tuple[Type[Any], ...], optional):* A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().
    *exclude_cruddals_interfaces (Tuple[str, ...], optional):* A tuple of interface names to be excluded if `DjangoAppCruddals` or `DjangoProjectCruddals` is used. Defaults to ().
    *registry (RegistryGlobal, optional):* The global registry object to be used for registering the generated GraphQL Schema. Defaults to None.
    *field_for_activate_deactivate (str, optional):* The name of the field used for activating/deactivating the object. Defaults to "is_active".
    ```
    """

    @classmethod
    def __init_subclass_with_meta__(
        cls,
        model: DjangoModel,
        config: Union[CruddalsBuilderConfig, None] = None,
        functions: Union[Tuple[FunctionType, ...], None] = None,
        exclude_functions: Union[Tuple[FunctionType, ...], None] = None,
        prefix: str = "",
        suffix: str = "",
        cruddals_interfaces: Tuple[Type[Any], ...] = (),
        exclude_cruddals_interfaces: Tuple[str, ...] = (),
        registry: Union[RegistryGlobal, None] = None,
        field_for_activate_deactivate: str = "is_active",
        **kwargs,
    ):
        """
        This method is called automatically when a subclass with a `Meta` class is defined. There is no need to call this method directly.

        Args:
            model (DjangoModel): The Django model class to be used for generate CRUDDALS operations.
            config (CruddalsBuilderConfig, optional): The configuration object for customizing the behavior of the CRUDDALS operations. Defaults and recommended to None.
            functions (Tuple[FunctionType, ...], optional): A tuple of strings with the name of functions ('create', 'read', 'update', 'delete', 'deactivate', 'activate', 'list', 'search') to be included in the generated GraphQL Schema. Defaults to None, in which case all functions will be included.
            exclude_functions (Tuple[FunctionType, ...], optional): A tuple of functions to be excluded from the generated GraphQL Schema. Defaults to None.
            prefix (str, optional): A prefix to be added to the generated GraphQL Types name. Defaults to "", in which case the model name will be used as is.
            suffix (str, optional): A suffix to be added to the generated GraphQL Types name. Defaults to "", in which case the model name will be used as is.
            cruddals_interfaces (Tuple[Type[Any], ...], optional): A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().
            exclude_cruddals_interfaces (Tuple[str, ...], optional): A tuple of interface names to be excluded from the generated GraphQL Schema. Defaults to ().
            registry (RegistryGlobal, optional): The global registry object to be used for registering the generated GraphQL Schema. Defaults to None, in which case a global registry will be used.
            field_for_activate_deactivate (str, optional): The name of the field used for activating/deactivating the object. Defaults to "is_active".

        Raises:
            AssertionError: If the `model` argument is not provided.
        """
        assert model, "model is required for DjangoModelCruddals"

        model_form_class = convert_model_to_model_form(
            model=model,
            prefix_for_name=prefix,
            suffix_for_name=suffix,
        )

        if not registry:
            registry = get_global_registry()

        if not config:
            config = get_cruddals_config_for_model(
                model,
                cls,
                model_form_class,
                registry,
                prefix,
                suffix,
                cruddals_interfaces,
                exclude_cruddals_interfaces,
                field_for_activate_deactivate,
            )

        super().__init_subclass_with_meta__(
            config=config,
            functions=functions,
            exclude_functions=exclude_functions,
            **kwargs,
        )

    @staticmethod
    def get_fields_for_output(model: DjangoModel):
        return get_model_fields_for_output(model, True)

    @staticmethod
    def get_fields_for_input(model: DjangoModel):
        return get_model_fields_for_input(model, TypesMutationEnum.CREATE_UPDATE.value)

    @staticmethod
    def input_field_converter_function(name, field, model: DjangoModel, registry):
        return convert_django_field_with_choices_to_create_update_input(
            name, field, model, registry, TypesMutationEnum.CREATE_UPDATE.value
        )

    @staticmethod
    def get_fields_for_create_input(model: DjangoModel):
        return get_model_fields_for_input(model, TypesMutationEnum.CREATE.value)

    @staticmethod
    def create_input_field_converter_function(name, field, model, registry):
        return convert_django_field_with_choices_to_create_update_input(
            name, field, model, registry, TypesMutationEnum.CREATE.value
        )

    @staticmethod
    def get_fields_for_update_input(model: DjangoModel):
        return get_model_fields_for_input(model, TypesMutationEnum.UPDATE.value)

    @staticmethod
    def update_input_field_converter_function(name, field, model, registry):
        return convert_django_field_with_choices_to_create_update_input(
            name, field, model, registry, TypesMutationEnum.UPDATE.value
        )

    @staticmethod
    def get_fields_for_filter(model: DjangoModel):
        return get_model_fields_for_output(model)

    @staticmethod
    def get_fields_for_order_by(model: DjangoModel):
        return get_model_fields_for_output(model)

__init_subclass_with_meta__ classmethod

__init_subclass_with_meta__(
    model,
    config=None,
    functions=None,
    exclude_functions=None,
    prefix="",
    suffix="",
    cruddals_interfaces=(),
    exclude_cruddals_interfaces=(),
    registry=None,
    field_for_activate_deactivate="is_active",
    **kwargs
)

This method is called automatically when a subclass with a Meta class is defined. There is no need to call this method directly.

Parameters:

Name Type Description Default
model Model

The Django model class to be used for generate CRUDDALS operations.

required
config CruddalsBuilderConfig

The configuration object for customizing the behavior of the CRUDDALS operations. Defaults and recommended to None.

None
functions Tuple[FunctionType, ...]

A tuple of strings with the name of functions ('create', 'read', 'update', 'delete', 'deactivate', 'activate', 'list', 'search') to be included in the generated GraphQL Schema. Defaults to None, in which case all functions will be included.

None
exclude_functions Tuple[FunctionType, ...]

A tuple of functions to be excluded from the generated GraphQL Schema. Defaults to None.

None
prefix str

A prefix to be added to the generated GraphQL Types name. Defaults to "", in which case the model name will be used as is.

''
suffix str

A suffix to be added to the generated GraphQL Types name. Defaults to "", in which case the model name will be used as is.

''
cruddals_interfaces Tuple[Type[Any], ...]

A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().

()
exclude_cruddals_interfaces Tuple[str, ...]

A tuple of interface names to be excluded from the generated GraphQL Schema. Defaults to ().

()
registry RegistryGlobal

The global registry object to be used for registering the generated GraphQL Schema. Defaults to None, in which case a global registry will be used.

None
field_for_activate_deactivate str

The name of the field used for activating/deactivating the object. Defaults to "is_active".

'is_active'

Raises:

Type Description
AssertionError

If the model argument is not provided.

Source code in graphene_django_cruddals/main.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
@classmethod
def __init_subclass_with_meta__(
    cls,
    model: DjangoModel,
    config: Union[CruddalsBuilderConfig, None] = None,
    functions: Union[Tuple[FunctionType, ...], None] = None,
    exclude_functions: Union[Tuple[FunctionType, ...], None] = None,
    prefix: str = "",
    suffix: str = "",
    cruddals_interfaces: Tuple[Type[Any], ...] = (),
    exclude_cruddals_interfaces: Tuple[str, ...] = (),
    registry: Union[RegistryGlobal, None] = None,
    field_for_activate_deactivate: str = "is_active",
    **kwargs,
):
    """
    This method is called automatically when a subclass with a `Meta` class is defined. There is no need to call this method directly.

    Args:
        model (DjangoModel): The Django model class to be used for generate CRUDDALS operations.
        config (CruddalsBuilderConfig, optional): The configuration object for customizing the behavior of the CRUDDALS operations. Defaults and recommended to None.
        functions (Tuple[FunctionType, ...], optional): A tuple of strings with the name of functions ('create', 'read', 'update', 'delete', 'deactivate', 'activate', 'list', 'search') to be included in the generated GraphQL Schema. Defaults to None, in which case all functions will be included.
        exclude_functions (Tuple[FunctionType, ...], optional): A tuple of functions to be excluded from the generated GraphQL Schema. Defaults to None.
        prefix (str, optional): A prefix to be added to the generated GraphQL Types name. Defaults to "", in which case the model name will be used as is.
        suffix (str, optional): A suffix to be added to the generated GraphQL Types name. Defaults to "", in which case the model name will be used as is.
        cruddals_interfaces (Tuple[Type[Any], ...], optional): A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().
        exclude_cruddals_interfaces (Tuple[str, ...], optional): A tuple of interface names to be excluded from the generated GraphQL Schema. Defaults to ().
        registry (RegistryGlobal, optional): The global registry object to be used for registering the generated GraphQL Schema. Defaults to None, in which case a global registry will be used.
        field_for_activate_deactivate (str, optional): The name of the field used for activating/deactivating the object. Defaults to "is_active".

    Raises:
        AssertionError: If the `model` argument is not provided.
    """
    assert model, "model is required for DjangoModelCruddals"

    model_form_class = convert_model_to_model_form(
        model=model,
        prefix_for_name=prefix,
        suffix_for_name=suffix,
    )

    if not registry:
        registry = get_global_registry()

    if not config:
        config = get_cruddals_config_for_model(
            model,
            cls,
            model_form_class,
            registry,
            prefix,
            suffix,
            cruddals_interfaces,
            exclude_cruddals_interfaces,
            field_for_activate_deactivate,
        )

    super().__init_subclass_with_meta__(
        config=config,
        functions=functions,
        exclude_functions=exclude_functions,
        **kwargs,
    )