Skip to content

Model helpers

django_rubble.utils.model_helpers

get_model_fields(model, *, fields=None, exclude=None)

Returns list of fields from model.

Cannot include both fields and exclude.

Parameters:

Name Type Description Default
model type[Model]

the model to get fields from

required
fields list[str] | None

list of fields to include, default is None

None
exclude list[str] | None

list of fields to exclude, default is None

None

Returns:

Type Description
list[Field]

list of fields

Source code in django_rubble\utils\model_helpers.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
70
71
72
73
def get_model_fields(
    model: type[Model],
    *,
    fields: list[str] | None = None,
    exclude: list[str] | None = None,
) -> list[Field]:
    """Returns list of fields from model.

    Cannot include both fields and exclude.

    Args:
      model: the model to get fields from
      fields: list of fields to include, default is None
      exclude: list of fields to exclude, default is None

    Returns:
      list of fields
    """
    if fields is not None and exclude is not None:
        msg = "Cannot specify both 'fields' and 'exclude'."
        raise ValueError(msg)

    field_list_full = cast(list[Field], list(model._meta.fields))  # noqa: SLF001  # `_meta` is private

    if fields is not None:
        return [model._meta.get_field(field) for field in fields]  # noqa: SLF001  # `_meta` is private
    if exclude is not None:
        return [field for field in field_list_full if field.name not in exclude]

    return field_list_full

get_model_label(model)

Get model's label as defined in Meta class.

Parameters:

Name Type Description Default
model Model

the model to get label from

required

Returns:

Type Description
str

model's label

Source code in django_rubble\utils\model_helpers.py
33
34
35
36
37
38
39
40
41
def get_model_label(model: Model) -> str:
    """Get model's label as defined in Meta class.

    Args:
        model: the model to get label from

    Returns:
        model's label"""
    return model._meta.label  # noqa: SLF001

get_model_name(model)

Get model's name as defined in Meta class.

Will return as all lower case

Parameters:

Name Type Description Default
model type[Model]

the model to get name from

required

Returns:

Type Description
str

model's name

Source code in django_rubble\utils\model_helpers.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
def get_model_name(model: type[Model]) -> str:
    """Get model's name as defined in Meta class.

    Will return as all lower case

    Args:
        model: the model to get name from

    Returns:
        model's name
    """
    return model._meta.model_name  # noqa: SLF001

get_model_verbose_name_plural(model)

Get model's verbose name as defined in Meta class.

Parameters:

Name Type Description Default
model type[Model]

the model to get verbose name from

required

Returns:

Type Description
str

model's verbose name

Source code in django_rubble\utils\model_helpers.py
21
22
23
24
25
26
27
28
29
30
def get_model_verbose_name_plural(model: type[Model]) -> str:
    """Get model's verbose name as defined in Meta class.

    Args:
        model: the model to get verbose name from

    Returns:
        model's verbose name
    """
    return model._meta.verbose_name_plural  # noqa: SLF001