Skip to main content

RelatedFieldWidgetWrapper

This class is a wrapper to a given widget to add the add icon for the admin interface.

Attributes

AttributeTypeDescription
template_namestring = admin/widgets/related_widget_wrapper.htmlThe 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

NameTypeDescription
widgetforms.WidgetThe base widget instance being wrapped.
reldjango.db.models.fields.reverse_related.ForeignObjectRelThe relationship object representing the connection to another model.
admin_sitedjango.contrib.admin.sites.AdminSiteThe admin site instance where the models are registered.
can_add_relatedboolean = NoneIndicates if the user can add a related object; defaults to checking if the model is registered.
can_change_relatedboolean = FalseIndicates if the user can change a related object.
can_delete_relatedboolean = FalseIndicates if the user can delete a related object.
can_view_relatedboolean = FalseIndicates 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

TypeDescription
boolTrue 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

TypeDescription
[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

TypeDescription
listThe list of choices available in the wrapped widget.

@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

NameTypeDescription
infotupleA tuple containing the app label and model name of the related object.
actionstrThe admin action to perform, such as 'add', 'change', or 'delete'.
*argsanyAdditional positional arguments used to reverse the URL, such as object IDs.

Returns

TypeDescription
strThe 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

NameTypeDescription
namestrThe name of the form field.
valueanyThe current value of the field to be rendered.
attrsdictHTML attributes to be applied to the rendered widget.

Returns

TypeDescription
dictA 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

NameTypeDescription
datadictThe dictionary of submitted POST/GET data.
filesdictThe dictionary of submitted file data.
namestrThe name of the field in the data dictionary.

Returns

TypeDescription
anyThe 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

NameTypeDescription
datadictThe dictionary of submitted POST/GET data.
filesdictThe dictionary of submitted file data.
namestrThe name of the field to check.

Returns

TypeDescription
boolTrue 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

NameTypeDescription
id_strThe base ID of the widget.

Returns

TypeDescription
strThe string ID used in the 'for' attribute of an HTML label tag.