RelatedFieldWidgetWrapper
This class is a wrapper to a given widget to add the add icon for the admin interface.
Attributes
| Attribute | Type | Description |
|---|---|---|
| template_name | string = admin/widgets/related_widget_wrapper.html | The path to the HTML template used to render the widget wrapper and its associated action icons. |
Constructor
Signature
def RelatedFieldWidgetWrapper(
widget: forms.Widget,
rel: django.db.models.fields.reverse_related.ForeignObjectRel,
admin_site: django.contrib.admin.sites.AdminSite,
can_add_related: boolean = None,
can_change_related: boolean = False,
can_delete_related: boolean = False,
can_view_related: boolean = False
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| widget | forms.Widget | The base widget instance being wrapped. |
| rel | django.db.models.fields.reverse_related.ForeignObjectRel | The relationship object representing the connection to another model. |
| admin_site | django.contrib.admin.sites.AdminSite | The admin site instance where the models are registered. |
| can_add_related | boolean = None | Indicates if the user can add a related object; defaults to checking if the model is registered. |
| can_change_related | boolean = False | Indicates if the user can change a related object. |
| can_delete_related | boolean = False | Indicates if the user can delete a related object. |
| can_view_related | boolean = False | Indicates if the user can view a related object. |
Methods
is_hidden()
@classmethod
def is_hidden() - > bool
Determines if the underlying widget is of a hidden type.
Returns
| Type | Description |
|---|---|
bool | True if the wrapped widget is hidden, False otherwise. |
media()
@classmethod
def media() - > [Media](../../../forms/widgets/media.md?sid=django_forms_widgets_media)
Retrieves the CSS and JavaScript assets required by the underlying widget.
Returns
| Type | Description |
|---|---|
[Media](../../../forms/widgets/media.md?sid=django_forms_widgets_media) | A Django Media object containing the paths to required static files. |
choices()
@classmethod
def choices() - > list
Gets or sets the available options for the underlying selection widget.
Returns
| Type | Description |
|---|---|
list | The list of choices available in the wrapped widget. |
get_related_url()
@classmethod
def get_related_url(
info: tuple,
action: str,
*args: any
) - > str
Constructs an administrative URL for a specific action on the related model.
Parameters
| Name | Type | Description |
|---|---|---|
| info | tuple | A tuple containing the app label and model name of the related object. |
| action | str | The admin action to perform, such as 'add', 'change', or 'delete'. |
| *args | any | Additional positional arguments used to reverse the URL, such as object IDs. |
Returns
| Type | Description |
|---|---|
str | The absolute URL path to the requested admin view. |
get_context()
@classmethod
def get_context(
name: str,
value: any,
attrs: dict
) - > dict
Builds the template context required to render the widget and its associated admin action links.
Parameters
| Name | Type | Description |
|---|---|---|
| name | str | The name of the form field. |
| value | any | The current value of the field to be rendered. |
| attrs | dict | HTML attributes to be applied to the rendered widget. |
Returns
| Type | Description |
|---|---|
dict | A dictionary containing the rendered widget HTML, permissions, and URLs for related object management. |
value_from_datadict()
@classmethod
def value_from_datadict(
data: dict,
files: dict,
name: str
) - > any
Extracts the field value from the submitted form data by delegating to the wrapped widget.
Parameters
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted POST/GET data. |
| files | dict | The dictionary of submitted file data. |
| name | str | The name of the field in the data dictionary. |
Returns
| Type | Description |
|---|---|
any | The value submitted for this field, typically a primary key or list of keys. |
value_omitted_from_data()
@classmethod
def value_omitted_from_data(
data: dict,
files: dict,
name: str
) - > bool
Checks if the field's value is missing from the submitted data by delegating to the wrapped widget.
Parameters
| Name | Type | Description |
|---|---|---|
| data | dict | The dictionary of submitted POST/GET data. |
| files | dict | The dictionary of submitted file data. |
| name | str | The name of the field to check. |
Returns
| Type | Description |
|---|---|
bool | True if the data does not contain a value for this field, False otherwise. |
id_for_label()
@classmethod
def id_for_label(
id_: str
) - > str
Returns the HTML ID attribute for the widget's label, delegating the logic to the wrapped widget.
Parameters
| Name | Type | Description |
|---|---|---|
| id_ | str | The base ID of the widget. |
Returns
| Type | Description |
|---|---|
str | The string ID used in the 'for' attribute of an HTML label tag. |