Models

Fields

class dj_kaos_utils.models.TwoPlacesDecimalField(*args, **kwargs)

Bases: DecimalField

A DecimalField with 2 decimal places

class dj_kaos_utils.models.MoneyField(*args, **kwargs)

Bases: TwoPlacesDecimalField

Model field representing an amount of money

class dj_kaos_utils.models.CaseInsensitiveFieldMixin

Bases: RegisterLookupMixin

Field mixin that uses case-insensitive lookup alternatives if they exist.

Example:
>>> class LowerCaseLookupCharField(CaseInsensitiveFieldMixin, models.CharField):
>>>     pass
class dj_kaos_utils.models.ToLowerCaseFieldMixin(verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=<class 'django.db.models.fields.NOT_PROVIDED'>, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text='', db_column=None, db_tablespace=None, auto_created=False, validators=(), error_messages=None)

Bases: Field

Always save the field as lowercase

class dj_kaos_utils.models.LowerCaseCharField(*args, db_collation=None, **kwargs)

Bases: CaseInsensitiveFieldMixin, ToLowerCaseFieldMixin, CharField

CharField that saves the values passed to it as lowercase.

QuerySets

Refer to QuerySets

Mixins

class dj_kaos_utils.models.HasAutoFields

Bases: object

Mixin for models that have fields that can be automatically set, for example from the value of other fields.

set_auto_fields()

Override this method to set the fields that need to be automatically set

class dj_kaos_utils.models.HasInitials

Bases: object

Add property initials to its inheritor classes. Take the value form the field defined by take_initials_from and return the initial letters of each word in it, capitalized. Interface using instance.initials.

property initials

Take the value form the field defined by take_initials_from and return the initial letters of each word in it, capitalized. Example: John Smith => JS :return: Initial letters of each word in the value from the field take_initials_from, capitalized.

class dj_kaos_utils.models.HasWarnings

Bases: object

Adds a method get_warnings useful to catch issues that aren’t worthy of throwing a ValidationError

Example:
>>> class MyModel(models.Model):
>>>     def get_warnings(self):
>>>         warnings = super().get_warnings()
>>>         if self.fulfills_condition():
>>>             return [*warnings, "New warning"]
>>>         return warnings
get_warnings() list[str | tuple[str, str]]

Override this method and append any warnings to the result of calling super()

Returns

A list of warnings. Each item in the list is either a tuple of (field_name, warning description) or just warning description.

Admin

class dj_kaos_utils.models.admin.HasWarningsAdmin

Bases: BaseModelAdmin

A Django ModelAdmin class to display warnings associated with an object of HasWarnings class.