Skip to content

Admin

Django admin utility module for field management and filtering.

This module provides utility functions and constants for configuring Django admin views, particularly focused on field filtering and display configurations.

Attributes:

Name Type Description
related_field_set Set[Type]

Set of Django field types that represent relations.

complex_field_set Set[Type]

Set of Django field types considered complex.

list_display_user_admin List[str]

Default display fields for User admin interface.

list_display_exclude List[Type]

Field types to exclude from admin list display.

list_filter_base List[str]

Base filter fields for standard admin views.

list_filter_base_public_contribute List[str]

Extended filter fields including verification status.

django_meta_get_fields(django_object, field_types, exclude=True)

Gets field names from a Django model based on specified field types.

Parameters:

Name Type Description Default
django_object Model

Django model instance or class to inspect.

required
field_types List[Type[Field]]

List of Django field types to filter by.

required
exclude bool

Whether to exclude or include the specified field types. Defaults to True.

True

Returns:

Type Description
List[str]

List[str]: List of field names that match the filtering criteria.

Example

fields = django_meta_get_fields(MyModel, [ManyToManyField], exclude=True) print(fields) ['id', 'name', 'description']

Source code in django_util/admin.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def django_meta_get_fields(
    django_object: "models.Model",
    field_types: List[Type["models.Field"]],
    exclude: bool = True,
) -> List[str]:
    """Gets field names from a Django model based on specified field types.

    Args:
        django_object (models.Model): Django model instance or class to inspect.
        field_types (List[Type[models.Field]]): List of Django field types to filter by.
        exclude (bool, optional): Whether to exclude or include the specified field types.
            Defaults to True.

    Returns:
        List[str]: List of field names that match the filtering criteria.

    Example:
        >>> fields = django_meta_get_fields(MyModel, [ManyToManyField], exclude=True)
        >>> print(fields)
        ['id', 'name', 'description']
    """
    return [
        field.name
        for field in django_object._meta.get_fields()
        if exclude != isinstance(field, tuple(field_types))
    ]

django_meta_get_fields_exclude(django_object, to_exclude=list_display_exclude)

Convenience wrapper for django_meta_get_fields that excludes specified field types.

Parameters:

Name Type Description Default
django_object Any

Django model instance or class to inspect.

required
to_exclude List[Type]

List of field types to exclude. Defaults to list_display_exclude.

list_display_exclude

Returns:

Type Description
List[str]

List[str]: List of field names excluding the specified field types.

Source code in django_util/admin.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def django_meta_get_fields_exclude(
    django_object: Any,
    to_exclude: List[Type] = list_display_exclude,
) -> List[str]:
    """Convenience wrapper for django_meta_get_fields that excludes specified field types.

    Args:
        django_object (Any): Django model instance or class to inspect.
        to_exclude (List[Type], optional): List of field types to exclude.
            Defaults to list_display_exclude.

    Returns:
        List[str]: List of field names excluding the specified field types.
    """
    return django_meta_get_fields(django_object, to_exclude, exclude=True)

django_meta_get_fields_include(django_object, to_include=[ManyToManyField])

Convenience wrapper for django_meta_get_fields that includes only specified field types.

Parameters:

Name Type Description Default
django_object Any

Django model instance or class to inspect.

required
to_include List[Type]

List of field types to include. Defaults to [ManyToManyField].

[ManyToManyField]

Returns:

Type Description
List[str]

List[str]: List of field names matching the specified field types.

Source code in django_util/admin.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def django_meta_get_fields_include(
    django_object: Any,
    to_include: List[Type] = [ManyToManyField],
) -> List[str]:
    """Convenience wrapper for django_meta_get_fields that includes only specified field types.

    Args:
        django_object (Any): Django model instance or class to inspect.
        to_include (List[Type], optional): List of field types to include.
            Defaults to [ManyToManyField].

    Returns:
        List[str]: List of field names matching the specified field types.
    """
    return django_meta_get_fields(django_object, to_include, exclude=False)