File: models.pyi

package info (click to toggle)
python-django-stubs 5.2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,832 kB
  • sloc: python: 5,185; makefile: 15; sh: 8
file content (331 lines) | stat: -rw-r--r-- 12,701 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from collections.abc import Callable, Collection, Container, Iterator, Mapping, MutableMapping, Sequence
from typing import Any, ClassVar, Generic, Literal, TypeAlias, TypeVar, overload
from uuid import UUID

from django.db import models
from django.db.models import ForeignKey
from django.db.models.base import Model
from django.db.models.fields import _AllLimitChoicesTo, _LimitChoicesTo
from django.db.models.manager import Manager
from django.db.models.query import QuerySet
from django.forms.fields import ChoiceField, Field, _ClassLevelWidgetT
from django.forms.forms import BaseForm, DeclarativeFieldsMetaclass
from django.forms.formsets import BaseFormSet
from django.forms.renderers import BaseRenderer
from django.forms.utils import ErrorList, _DataT, _FilesT
from django.forms.widgets import Widget
from django.utils.choices import BaseChoiceIterator, CallableChoiceIterator, _ChoicesCallable, _ChoicesInput
from django.utils.datastructures import _PropertyDescriptor
from django.utils.functional import _StrOrPromise

ALL_FIELDS: Literal["__all__"]

_Fields: TypeAlias = Collection[str] | Literal["__all__"]
_Widgets: TypeAlias = dict[str, type[Widget] | Widget]

_Labels: TypeAlias = dict[str, str]
_HelpTexts: TypeAlias = dict[str, str]
_ErrorMessages: TypeAlias = dict[str, dict[str, str]]
_FormFieldCallback: TypeAlias = Callable[[models.Field], Field]

_M = TypeVar("_M", bound=Model)
_ParentM = TypeVar("_ParentM", bound=Model)

def construct_instance(
    form: BaseForm, instance: _M, fields: Container[str] | None = None, exclude: Container[str] | None = None
) -> _M: ...
def model_to_dict(instance: Model, fields: _Fields | None = None, exclude: _Fields | None = None) -> dict[str, Any]: ...
def apply_limit_choices_to_to_formfield(formfield: Field) -> None: ...
def fields_for_model(
    model: type[Model],
    fields: _Fields | None = None,
    exclude: _Fields | None = None,
    widgets: _Widgets | None = None,
    formfield_callback: _FormFieldCallback | None = None,
    localized_fields: _Fields | None = None,
    labels: _Labels | None = None,
    help_texts: _HelpTexts | None = None,
    error_messages: _ErrorMessages | None = None,
    field_classes: Mapping[str, type[Field]] | None = None,
    *,
    apply_limit_choices_to: bool = True,
    form_declared_fields: _Fields | None = None,
) -> dict[str, Any]: ...

class ModelFormOptions(Generic[_M]):
    model: type[_M]
    fields: _Fields | None
    exclude: _Fields | None
    widgets: _Widgets | None
    localized_fields: _Fields | None
    labels: _Labels | None
    help_texts: _HelpTexts | None
    error_messages: _ErrorMessages | None
    field_classes: dict[str, type[Field]] | None
    formfield_callback: _FormFieldCallback | None
    def __init__(self, options: type | None = None) -> None: ...

class ModelFormMetaclass(DeclarativeFieldsMetaclass): ...

class BaseModelForm(BaseForm, Generic[_M]):
    instance: _M
    _meta: ModelFormOptions[_M]
    def __init__(
        self,
        data: _DataT | None = None,
        files: _FilesT | None = None,
        auto_id: bool | str = "id_%s",
        prefix: str | None = None,
        initial: MutableMapping[str, Any] | None = None,
        error_class: type[ErrorList] = ...,
        label_suffix: str | None = None,
        empty_permitted: bool = False,
        instance: _M | None = None,
        use_required_attribute: bool | None = None,
        renderer: BaseRenderer | None = None,
    ) -> None: ...
    def validate_unique(self) -> None: ...
    def save(self, commit: bool = True) -> _M: ...
    def save_m2m(self) -> None: ...

class ModelForm(BaseModelForm[_M], metaclass=ModelFormMetaclass):
    base_fields: ClassVar[dict[str, Field]]
    declared_fields: ClassVar[dict[str, Field]]

def modelform_factory(
    model: type[_M],
    form: type[ModelForm[_M]] = ...,
    fields: _Fields | None = None,
    exclude: _Fields | None = None,
    formfield_callback: _FormFieldCallback | None = None,
    widgets: _Widgets | None = None,
    localized_fields: _Fields | None = None,
    labels: _Labels | None = None,
    help_texts: _HelpTexts | None = None,
    error_messages: _ErrorMessages | None = None,
    field_classes: Mapping[str, type[Field]] | None = None,
) -> type[ModelForm[_M]]: ...

_ModelFormT = TypeVar("_ModelFormT", bound=ModelForm)

class BaseModelFormSet(BaseFormSet[_ModelFormT], Generic[_M, _ModelFormT]):
    model: type[_M]
    edit_only: bool
    unique_fields: Collection[str]
    queryset: QuerySet[_M] | None
    initial_extra: Sequence[dict[str, Any]] | None
    def __init__(
        self,
        data: _DataT | None = None,
        files: _FilesT | None = None,
        auto_id: str = "id_%s",
        prefix: str | None = None,
        queryset: QuerySet[_M] | None = None,
        *,
        initial: Sequence[dict[str, Any]] | None = None,
        **kwargs: Any,
    ) -> None: ...
    def initial_form_count(self) -> int: ...
    def get_queryset(self) -> QuerySet[_M]: ...
    def save_new(self, form: _ModelFormT, commit: bool = True) -> _M: ...
    def save_existing(self, form: _ModelFormT, obj: _M, commit: bool = True) -> _M: ...
    def delete_existing(self, obj: _M, commit: bool = True) -> None: ...
    saved_forms: list[_ModelFormT]
    def save_m2m(self) -> None: ...
    def save(self, commit: bool = True) -> list[_M]: ...
    def clean(self) -> None: ...
    def validate_unique(self) -> None: ...
    def get_unique_error_message(self, unique_check: Sequence[str]) -> str: ...
    def get_date_error_message(self, date_check: tuple[str, Literal["date", "year", "month"], str, str]) -> str: ...
    def get_form_error(self) -> str: ...
    changed_objects: list[tuple[_M, list[str]]]
    deleted_objects: list[_M]
    def save_existing_objects(self, commit: bool = True) -> list[_M]: ...
    new_objects: list[_M]
    def save_new_objects(self, commit: bool = True) -> list[_M]: ...
    def add_fields(self, form: _ModelFormT, index: int | None) -> None: ...

def modelformset_factory(
    model: type[_M],
    form: type[_ModelFormT] = ...,
    formfield_callback: _FormFieldCallback | None = None,
    formset: type[BaseModelFormSet] = ...,
    extra: int = 1,
    can_delete: bool = False,
    can_order: bool = False,
    max_num: int | None = None,
    fields: _Fields | None = None,
    exclude: _Fields | None = None,
    widgets: _Widgets | None = None,
    validate_max: bool = False,
    localized_fields: _Fields | None = None,
    labels: _Labels | None = None,
    help_texts: _HelpTexts | None = None,
    error_messages: _ErrorMessages | None = None,
    min_num: int | None = None,
    validate_min: bool = False,
    field_classes: Mapping[str, type[Field]] | None = None,
    absolute_max: int | None = None,
    can_delete_extra: bool = True,
    renderer: BaseRenderer | None = None,
    edit_only: bool = False,
) -> type[BaseModelFormSet[_M, _ModelFormT]]: ...

class BaseInlineFormSet(BaseModelFormSet[_M, _ModelFormT], Generic[_M, _ParentM, _ModelFormT]):
    instance: _ParentM
    save_as_new: bool
    unique_fields: Collection[str]
    fk: ForeignKey  # set by inlineformset_set
    def __init__(
        self,
        data: _DataT | None = None,
        files: _FilesT | None = None,
        instance: _ParentM | None = None,
        save_as_new: bool = False,
        prefix: str | None = None,
        queryset: QuerySet[_M] | None = None,
        **kwargs: Any,
    ) -> None: ...
    def initial_form_count(self) -> int: ...
    @classmethod
    def get_default_prefix(cls) -> str: ...
    def save_new(self, form: _ModelFormT, commit: bool = True) -> _M: ...
    def add_fields(self, form: _ModelFormT, index: int | None) -> None: ...
    def get_unique_error_message(self, unique_check: Sequence[str]) -> str: ...

def inlineformset_factory(
    parent_model: type[_ParentM],
    model: type[_M],
    form: type[_ModelFormT] = ...,
    formset: type[BaseInlineFormSet] = ...,
    fk_name: str | None = None,
    fields: _Fields | None = None,
    exclude: _Fields | None = None,
    extra: int = 3,
    can_order: bool = False,
    can_delete: bool = True,
    max_num: int | None = None,
    formfield_callback: _FormFieldCallback | None = None,
    widgets: _Widgets | None = None,
    validate_max: bool = False,
    localized_fields: Sequence[str] | None = None,
    labels: _Labels | None = None,
    help_texts: _HelpTexts | None = None,
    error_messages: _ErrorMessages | None = None,
    min_num: int | None = None,
    validate_min: bool = False,
    field_classes: Mapping[str, type[Field]] | None = None,
    absolute_max: int | None = None,
    can_delete_extra: bool = True,
    renderer: BaseRenderer | None = None,
    edit_only: bool = False,
) -> type[BaseInlineFormSet[_M, _ParentM, _ModelFormT]]: ...

class InlineForeignKeyField(Field):
    disabled: bool
    help_text: _StrOrPromise
    required: bool
    show_hidden_initial: bool
    widget: _ClassLevelWidgetT
    parent_instance: Model
    pk_field: bool
    to_field: str | None
    def __init__(
        self,
        parent_instance: Model,
        *args: Any,
        pk_field: bool = False,
        to_field: str | None = None,
        **kwargs: Any,
    ) -> None: ...
    def clean(self, value: Any) -> Model: ...
    def has_changed(self, initial: Any, data: Any) -> bool: ...

class ModelChoiceIteratorValue:
    def __init__(self, value: Any, instance: Model) -> None: ...

class ModelChoiceIterator(BaseChoiceIterator):
    field: ModelChoiceField
    queryset: QuerySet
    def __init__(self, field: ModelChoiceField) -> None: ...
    def __iter__(self) -> Iterator[tuple[ModelChoiceIteratorValue | str, str]]: ...
    def __len__(self) -> int: ...
    def __bool__(self) -> bool: ...
    def choice(self, obj: Model) -> tuple[ModelChoiceIteratorValue, str]: ...

class ModelChoiceField(ChoiceField, Generic[_M]):
    disabled: bool
    help_text: _StrOrPromise
    required: bool
    show_hidden_initial: bool
    validators: list[Any]
    iterator: type[ModelChoiceIterator]
    empty_label: _StrOrPromise | None
    queryset: QuerySet[_M] | None
    limit_choices_to: _AllLimitChoicesTo | None
    to_field_name: str | None
    def __init__(
        self,
        queryset: Manager[_M] | QuerySet[_M] | None,
        *,
        empty_label: _StrOrPromise | None = "---------",
        required: bool = True,
        widget: Widget | type[Widget] | None = None,
        label: _StrOrPromise | None = None,
        initial: Any | None = None,
        help_text: _StrOrPromise = "",
        to_field_name: str | None = None,
        limit_choices_to: _AllLimitChoicesTo | None = None,
        blank: bool = False,
        **kwargs: Any,
    ) -> None: ...
    def validate_no_null_characters(self, value: Any) -> None: ...
    def get_limit_choices_to(self) -> _LimitChoicesTo: ...
    def label_from_instance(self, obj: _M) -> str: ...
    choices: _PropertyDescriptor[
        _ChoicesInput | _ChoicesCallable | CallableChoiceIterator,
        _ChoicesInput | CallableChoiceIterator | ModelChoiceIterator,
    ]
    def prepare_value(self, value: Any) -> Any: ...
    def to_python(self, value: Any | None) -> _M | None: ...
    def validate(self, value: _M | None) -> None: ...
    def has_changed(self, initial: Model | int | str | UUID | None, data: int | str | None) -> bool: ...

class ModelMultipleChoiceField(ModelChoiceField[_M]):
    disabled: bool
    empty_label: _StrOrPromise | None
    help_text: _StrOrPromise
    required: bool
    show_hidden_initial: bool
    widget: _ClassLevelWidgetT
    hidden_widget: type[Widget]
    def __init__(self, queryset: Manager[_M] | QuerySet[_M] | None, **kwargs: Any) -> None: ...
    def to_python(self, value: Any) -> list[_M]: ...  # type: ignore[override]
    def clean(self, value: Any) -> QuerySet[_M]: ...
    def prepare_value(self, value: Any) -> Any: ...
    def has_changed(self, initial: Collection[Any] | None, data: Collection[Any] | None) -> bool: ...  # type: ignore[override]

def modelform_defines_fields(form_class: type[ModelForm]) -> bool: ...
@overload
def _get_foreign_key(
    parent_model: type[Model], model: type[Model], fk_name: str | None = None, can_fail: Literal[True] = True
) -> ForeignKey | None: ...
@overload
def _get_foreign_key(
    parent_model: type[Model], model: type[Model], fk_name: str | None = None, can_fail: Literal[False] = False
) -> ForeignKey: ...

__all__ = (
    "ALL_FIELDS",
    "BaseInlineFormSet",
    "BaseModelForm",
    "BaseModelFormSet",
    "ModelChoiceField",
    "ModelForm",
    "ModelMultipleChoiceField",
    "fields_for_model",
    "inlineformset_factory",
    "model_to_dict",
    "modelform_factory",
    "modelformset_factory",
)