-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathsparsevec.py
More file actions
60 lines (45 loc) · 2.04 KB
/
Copy pathsparsevec.py
File metadata and controls
60 lines (45 loc) · 2.04 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from django import forms
from django.db.models import Field
from typing import Any
from .. import SparseVector
# https://docs.djangoproject.com/en/5.0/howto/custom-model-fields/
class SparseVectorField(Field):
description = 'Sparse vector'
empty_strings_allowed = False
def __init__(self, *args: Any, dimensions: int | None = None, **kwargs: Any):
self.dimensions = dimensions
super().__init__(*args, **kwargs)
def deconstruct(self) -> tuple[Any, Any, Any, Any]:
name, path, args, kwargs = super().deconstruct()
if self.dimensions is not None:
kwargs['dimensions'] = self.dimensions
return name, path, args, kwargs
def db_type(self, connection: Any) -> str:
if self.dimensions is None:
return 'sparsevec'
return 'sparsevec(%d)' % self.dimensions
def from_db_value(self, value: Any, expression: Any, connection: Any) -> SparseVector | None:
return SparseVector._from_db(value)
def to_python(self, value: Any) -> SparseVector | None:
return SparseVector._from_db(value)
def get_prep_value(self, value: Any) -> str | None:
return SparseVector._to_db(value)
def value_to_string(self, obj: Any) -> str | None:
return self.get_prep_value(self.value_from_object(obj))
def formfield(self, form_class: Any = None, choices_form_class: Any = None, **kwargs: Any) -> forms.Field:
return super().formfield(
form_class=SparseVectorFormField if form_class is None else form_class,
choices_form_class=choices_form_class,
**kwargs
)
class SparseVectorWidget(forms.TextInput):
def format_value(self, value: Any) -> str | None:
if isinstance(value, SparseVector):
value = value.to_text()
return super().format_value(value)
class SparseVectorFormField(forms.CharField):
widget = SparseVectorWidget
def to_python(self, value: Any) -> Any:
if isinstance(value, str) and value == '':
return None
return super().to_python(value)