fairdm.utils.inspection#
Field inspection and smart defaults for FairDM models.
Provides the FieldInspector class that introspects Django models to detect field types, suggest widgets and filters, and provide smart defaults for configuration.
Module Contents#
Classes#
Introspects Django models to provide smart field detection and configuration. |
API#
- class FieldInspector(model: type[django.db.models.Model])[source]#
Introspects Django models to provide smart field detection and configuration.
The FieldInspector analyzes a model’s fields and provides intelligent defaults for forms, tables, filters, and admin interfaces based on field types and patterns.
Args: model: The Django model class to inspect.
Attributes: ALWAYS_EXCLUDE: Field names always left out of auto-detection. EXCLUDE_SUFFIXES: Name suffixes left out of auto-detection. NEVER_DEFAULT: Names never included by default, whatever their type. DATE_PATTERNS: Name fragments that mark a date field for grouping. STATUS_PATTERNS: Name fragments that mark a status field for grouping. RELATION_TYPES: The field classes that count as relations.
Example: >>> inspector = FieldInspector(MySampleModel) >>> safe_fields = inspector.get_safe_fields() >>> date_fields = inspector.get_date_fields() >>> widget = inspector.suggest_widget(“collected_at”)
Initialization
- ALWAYS_EXCLUDE#
[‘id’, ‘polymorphic_ctype’, ‘polymorphic_ctype_id’]
- EXCLUDE_SUFFIXES#
(‘_ptr’, ‘_ptr_id’)
- NEVER_DEFAULT#
(‘password’,)
- DATE_PATTERNS#
[‘_at’, ‘date’, ‘date’, ‘created’, ‘modified’, ‘updated’]
- STATUS_PATTERNS#
[‘status’, ‘state’, ‘published’, ‘active’, ‘enabled’]
- RELATION_TYPES#
()
- get_field(field_name: str) django.db.models.Field | None[source]#
Get a specific field by name.
Args: field_name: Name of the field.
Returns: The field instance, or
Noneif not found.
- has_field(field_name: str) bool[source]#
Check if the model has a field with the given name.
Args: field_name: Name of the field.
Returns:
Trueif the field exists,Falseotherwise.
- should_exclude_field(field_name: str) bool[source]#
Return whether a field stays out of the framework’s own choice of fields.
This is the one implementation of that rule: the primary key, polymorphic type columns, inheritance pointers, automatic timestamps, anything non-editable, reverse relations, many-to-many fields with an explicit through model, and the names in NEVER_DEFAULT.
A portal that wants an excluded field says so in its own field list. This decides only what happens when it says nothing.
Args: field_name: Name of the field.
Returns:
Truewhen the field is left out by default.
- get_default_fields(exclude: list[str] | None = None) list[str][source]#
Return the framework’s own choice of fields for this model.
This is what a component is built from when a portal declares no field list of its own. There is one implementation of it, here, because two copies disagreeing meant the API and the admin could show different default fields for the same model.
Args: exclude: Field names to leave out as well.
Returns: The default field names.
- get_safe_fields(exclude: list[str] | None = None) list[str][source]#
Deprecated name for :meth:
get_default_fields.
- resolve_path(path: str) tuple[bool, str | None][source]#
Walk a field path, one segment at a time.
Used by registration to refuse a path before it becomes a broken page.
Args: path: A field path using double underscores, such as
"dataset__name".Returns: Whether the whole path resolves and, when it does not, the reason: either the prefix that is not a relation, or
Nonewhen a segment does not exist.
- close_matches(name: str, limit: int = 3) list[str][source]#
Return field names close enough to
nameto be worth suggesting.Args: name: The misspelled name. limit: The most matches to return.
Returns: Up to
limitsimilar field names.
- get_all_field_names() list[str][source]#
Get all field names on the model.
Returns: All field names.
- get_date_fields() list[str][source]#
Get fields that represent dates or datetimes.
Returns: The date, datetime and time field names.
- get_choice_fields() list[str][source]#
Get fields that have choices defined.
Returns: The names of fields with choices.
- get_relation_fields() list[str][source]#
Get foreign key and many-to-many relationship fields.
Returns: The relationship field names.
- get_text_fields() list[str][source]#
Get text fields (CharField, TextField).
Returns: The text field names.
- get_numeric_fields() list[str][source]#
Get numeric fields (Integer, Float, Decimal).
Returns: The numeric field names.
- suggest_widget(field_name: str) str | None[source]#
Suggest an appropriate widget for a field.
Args: field_name: Name of the field.
Returns: The widget class name, or
Nonefor the default.
- suggest_filter_type(field_name: str) str | None[source]#
Suggest an appropriate filter type for a field.
Args: field_name: Name of the field.
Returns: The filter class name, or
Nonefor the default.
- get_default_list_fields() list[str][source]#
Get default fields suitable for list and table display.
Includes the name field if present, plus a few other key fields, excluding long text fields and relations.
Returns: The field names suitable for tables.
- get_default_filter_fields() list[str][source]#
Get default fields suitable for filtering.
Includes date fields, choice fields, boolean fields, and foreign keys.
Returns: The field names suitable for filters.