-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathbit.py
More file actions
37 lines (29 loc) · 1.21 KB
/
Copy pathbit.py
File metadata and controls
37 lines (29 loc) · 1.21 KB
1
2
3
4
5
6
7
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
35
36
37
from django import forms
from django.db.models import Field
from typing import Any
# https://docs.djangoproject.com/en/5.0/howto/custom-model-fields/
class BitField(Field):
description = 'Bit string'
def __init__(self, *args: Any, length: int | None = None, **kwargs: Any) -> None:
self.length = length
super().__init__(*args, **kwargs)
def deconstruct(self) -> tuple[Any, Any, Any, Any]:
name, path, args, kwargs = super().deconstruct()
if self.length is not None:
kwargs['length'] = self.length
return name, path, args, kwargs
def db_type(self, connection: Any) -> str:
if self.length is None:
return 'bit'
return 'bit(%d)' % self.length
def formfield(self, form_class: Any = None, choices_form_class: Any = None, **kwargs: Any) -> forms.Field:
return super().formfield(
form_class=BitFormField if form_class is None else form_class,
choices_form_class=choices_form_class,
**kwargs
)
class BitFormField(forms.CharField):
def to_python(self, value: Any) -> Any:
if isinstance(value, str) and value == '':
return None
return super().to_python(value)