Skip to content

Fields

NoParamURLField

Bases: URLField

A URLField that stores URLs without query parameters or fragments.

This field extends Django's URLField to store only the domain and path components of URLs, stripping away query parameters and fragments.

Parameters:

Name Type Description Default
*args

Variable length argument list passed to URLField.

required
**kwargs

Arbitrary keyword arguments passed to URLField.

required

Returns:

Name Type Description
str

The processed URL containing only domain and path.

None

If the input cannot be parsed as a valid URL.

Raises:

Type Description
ValidationError

If the URL doesn't contain both domain and path components.

Examples:

>>> field = NoParamURLField()
>>> field.get_prep_value('https://example.com/path?query=1#fragment')
'example.com/path'
Source code in django_util/fields.py
37
38
39
40
41
42
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
69
70
71
72
73
74
75
class NoParamURLField(models.URLField):
    """A URLField that stores URLs without query parameters or fragments.

    This field extends Django's URLField to store only the domain and path components
    of URLs, stripping away query parameters and fragments.

    Args:
        *args: Variable length argument list passed to URLField.
        **kwargs: Arbitrary keyword arguments passed to URLField.

    Returns:
        str: The processed URL containing only domain and path.
        None: If the input cannot be parsed as a valid URL.

    Raises:
        ValidationError: If the URL doesn't contain both domain and path components.

    Examples:
        >>> field = NoParamURLField()
        >>> field.get_prep_value('https://example.com/path?query=1#fragment')
        'example.com/path'
    """

    def get_prep_value(self, value: Any) -> str | None:
        try:
            parsed = urlparse(value)
            return parsed.netloc + parsed.path
        except Exception:
            return None

    def validate(self, value: Any, model_instance) -> None:
        if value and not (urlparse(value).netloc and urlparse(value).path):
            from django.core.exceptions import ValidationError

            raise ValidationError("URL must contain both domain and path")
        super().validate(value, model_instance)

    def __repr__(self) -> str:
        return f"{self.__class__.__name__}(max_length={self.max_length})"

UpperTextField

Bases: TextField

A TextField that converts input to uppercase and strips whitespace.

This field extends Django's TextField to automatically convert stored values to uppercase and remove leading/trailing whitespace.

Parameters:

Name Type Description Default
*args

Variable length argument list passed to TextField.

required
**kwargs

Arbitrary keyword arguments passed to TextField.

required

Returns:

Name Type Description
str

The processed string in uppercase with stripped whitespace.

None

If the input cannot be converted to a string.

Examples:

>>> field = UpperTextField()
>>> field.get_prep_value(" hello world ")
'HELLO WORLD'
>>> field.get_prep_value(None)
None
Source code in django_util/fields.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class UpperTextField(models.TextField):
    """A TextField that converts input to uppercase and strips whitespace.

    This field extends Django's TextField to automatically convert stored values
    to uppercase and remove leading/trailing whitespace.

    Args:
        *args: Variable length argument list passed to TextField.
        **kwargs: Arbitrary keyword arguments passed to TextField.

    Returns:
        str: The processed string in uppercase with stripped whitespace.
        None: If the input cannot be converted to a string.

    Examples:
        >>> field = UpperTextField()
        >>> field.get_prep_value(" hello world ")
        'HELLO WORLD'
        >>> field.get_prep_value(None)
        None
    """

    def get_prep_value(self, value: Any) -> str | None:
        try:
            return str(value).strip().upper()
        except (TypeError, AttributeError):
            return None