Skip to content

Messages

django_rubble.checks.messages

Message functions for the checks module.

This module provides functions that return error messages for the checks module.

Available functions:

  • must_be: Return a list of errors for a missing or incorrect option value.
  • refer_to_missing_field: Return a list of errors for a missing field.

must_be(type_description, option, obj, error_id)

Return a list of errors for a missing or incorrect option value.

Examples:

>>> must_be("a list", "natural_key_fields", cls, "rubble.E002")
[Error: The value of 'natural_key_fields' must be a list.]

Parameters:

Name Type Description Default
type_description str

The description of the required type.

required
option str

The name of the option.

required
obj object

The object to which the option belongs.

required
error_id str

The error ID.

required

Returns:

Type Description
list[Error]

list[Error]: A list of errors

Source code in django_rubble\checks\messages.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def must_be(
    type_description: str, option: str, obj: object, error_id: str
) -> list[checks.Error]:
    """Return a list of errors for a missing or incorrect option value.

    Examples:
        >>> must_be("a list", "natural_key_fields", cls, "rubble.E002")
        [Error: The value of 'natural_key_fields' must be a list.]

    Args:
        type_description (str): The description of the required type.
        option (str): The name of the option.
        obj (object): The object to which the option belongs.
        error_id (str): The error ID.

    Returns:
        list[Error]: A list of errors
    """
    return [
        checks.Error(
            f"The value of '{option}' must be {type_description}.",
            obj=obj.__class__,
            id=error_id,
        ),
    ]

refer_to_missing_field(field_name, option, obj, error_id)

Return a list of errors for a missing field.

Examples:

>>> refer_to_missing_field("foo", "bar", MyModel, "rubble.E003")
[Error: The value of 'bar' refers to 'foo', which is not a field of 'MyModel'.]

Parameters:

Name Type Description Default
field_name str

The name of the missing field.

required
option str

The name of the option.

required
obj type[Model]

The model to which the option belongs.

required
error_id str

The error ID.

required

Returns:

Type Description
list[Error]

list[Error]: A list of errors

Source code in django_rubble\checks\messages.py
43
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
def refer_to_missing_field(
    field_name: str, option: str, obj: type[models.Model], error_id: str
) -> list[checks.Error]:
    """Return a list of errors for a missing field.

    Examples:
        >>> refer_to_missing_field("foo", "bar", MyModel, "rubble.E003")
        [Error: The value of 'bar' refers to 'foo', which is not a field of 'MyModel'.]

    Args:
        field_name (str): The name of the missing field.
        option (str): The name of the option.
        obj (type[models.Model]): The model to which the option belongs.
        error_id (str): The error ID.

    Returns:
        list[Error]: A list of errors
    """
    return [
        checks.Error(
            f"The value of '{option}' refers to '{field_name}',"
            f" which is not a field of '{get_model_label(obj)}'.",
            obj=obj,
            id=error_id,
        ),
    ]