onconova.core.serialization.fields
DJANGO_TO_PYDANTIC_TYPES
module-attribute
¶
FILTERS_MAP
module-attribute
¶
UserModel
module-attribute
¶
SchemaFieldDefinition(name, python_type, field_info, resolver_fcn=None)
¶
Represents a field definition in a schema, including its type, alias, and additional metadata.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the field. |
python_type |
Any
|
The Python type of the field. |
field_info |
FieldInfo
|
Pydantic FieldInfo object containing metadata for the field. |
resolver_fcn |
Callable[..., Any] | None
|
Optional function to resolve or transform the field value. |
Source code in onconova/core/serialization/fields.py
SchemaFieldInfo(resolver_fcn, python_type, serialization_alias, default, default_factory, extras)
¶
Holds metadata and configuration for a schema field used in serialization.
Attributes:
Name | Type | Description |
---|---|---|
resolver_fcn |
Callable[..., Any] | None
|
Optional function to resolve or transform the field value. |
python_type |
Any
|
The expected Python type of the field. |
serialization_alias |
str
|
The name to use for this field during serialization. |
default |
Any
|
The default value for the field if not provided. |
default_factory |
Callable[[], Any] | None
|
Optional factory function to generate a default value. |
extras |
Dict[str, Any]
|
Additional metadata or options for the field. |
Source code in onconova/core/serialization/fields.py
create_field_info(field, serialization_alias, default=PydanticUndefined, optional=False, expanded=False, default_factory=None, description=None, **json_schema_extra)
¶
Creates and returns a FieldInfo object for a given ORM field, configuring serialization and schema metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Field
|
The ORM field instance to extract information from. |
required |
|
str
|
The alias to use for serialization. |
required |
|
Any
|
The default value for the field. Defaults to PydanticUndefined. |
PydanticUndefined
|
|
bool
|
Whether the field is optional. If True, sets default to None. Defaults to False. |
False
|
|
bool
|
Whether the field should be marked as expanded in the schema. Defaults to False. |
False
|
|
callable
|
A callable that returns the default value for the field. |
None
|
|
Any
|
Additional keyword arguments to include in the JSON schema. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
FieldInfo |
FieldInfo
|
An object containing metadata for the field, suitable for use with Pydantic models. |
Notes
- If the field is marked as blank, null, or optional, the default is set to None.
- The function extracts title and description from the field's verbose_name and help_text, if available.
- Additional JSON schema metadata can be passed via keyword arguments.
Source code in onconova/core/serialization/fields.py
get_related_field_type(field, related_model, serialization_alias)
¶
Determines the serialization type and resolver for a Django related field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Field
|
The Django model field representing the relation. |
required |
|
type[Model]
|
The related Django model class. |
required |
|
str
|
The base alias to use for serialization. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple[Callable | None, Any, str, Dict[str, Any]]
|
|
Notes
- Handles special cases for models inheriting from
CodedConceptModel
andUserModel
. - For other models, determines the appropriate resolver and type based on the field's relation type.
Source code in onconova/core/serialization/fields.py
get_schema_field(field, *, expand=None, optional=False, exclude_related_fields=None)
¶
Generates a schema field definition from a Django model field.
Handles both relation and non-relation fields, including support for array fields. Optionally expands related fields and marks fields as optional or nullable based on field attributes or explicit parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Field
|
The Django model field to convert. |
required |
|
str | None
|
Name of the related field to expand, if any. |
None
|
|
bool
|
Whether the field should be considered optional. |
False
|
|
List[str] | None
|
List of related field names to exclude from expansion. |
None
|
Returns:
Name | Type | Description |
---|---|---|
SchemaFieldDefinition |
SchemaFieldDefinition
|
The generated schema field definition with type, alias, and resolver. |
Source code in onconova/core/serialization/fields.py
get_schema_field_filters(field_name, field)
¶
Source code in onconova/core/serialization/fields.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
|
process_non_relation_field(field)
¶
Processes a non-relation Django model field and returns a SchemaFieldInfo object containing metadata for serialization.
This function determines the appropriate Python type, serialization alias, default value or factory, and any extra metadata required for the field. It handles special cases for custom fields such as MeasurementField, DateRangeField, IntegerRangeField, BigIntegerRangeField, and CharField with choices (enums). For other field types, it falls back to a mapping from Django internal types to Pydantic types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Field
|
The Django model field to process. |
required |
Returns:
Name | Type | Description |
---|---|---|
SchemaFieldInfo |
SchemaFieldInfo
|
An object containing information about the field for schema |
SchemaFieldInfo
|
generation and serialization. |
Source code in onconova/core/serialization/fields.py
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 |
|
process_relation_field(field, exclude_related_fields=None, expand=None)
¶
Processes a Django model relation field and returns a SchemaFieldInfo object describing how the field should be serialized for API schemas.
Depending on the expand
parameter, the function either expands the related model
into a nested schema or represents the relation as a reference (e.g., an ID or list of IDs).
Handles both single and multiple relations (ForeignKey, OneToOne, ManyToMany, etc.).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Field
|
The Django model relation field to process. |
required |
|
List[str] | None
|
Optional list of related model field names to exclude from the schema. |
None
|
|
str | None
|
If provided, expands the related model using the given schema name; otherwise, uses a reference. |
None
|
Returns:
Name | Type | Description |
---|---|---|
SchemaFieldInfo |
SchemaFieldInfo
|
An object containing information about how to serialize the relation field, |
SchemaFieldInfo
|
including the resolver function, Python type, serialization alias, default values, and any extra metadata. |
Source code in onconova/core/serialization/fields.py
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 |
|