Skip to main content

ForeignKeyRawIdWidget

A Widget for displaying ForeignKeys in the "raw_id" interface rather than in a < select > box.

Attributes

AttributeTypeDescription
template_namestring = admin/widgets/foreign_key_raw_id.htmlPath to the HTML template used to render the raw ID input and its associated lookup link.

Constructor

Signature

def ForeignKeyRawIdWidget(
rel: object,
admin_site: [AdminSite](../sites/adminsite.md?sid=django_contrib_admin_sites_adminsite),
attrs: dict = None,
using: str = None
)

Parameters

NameTypeDescription
relobjectThe relationship object (Foreign Object Rel) that the widget is for.
admin_site[AdminSite](../sites/adminsite.md?sid=django_contrib_admin_sites_adminsite)The AdminSite instance the widget is associated with.
attrsdict = NoneOptional dictionary of HTML attributes to be set on the widget.
usingstr = NoneThe database alias to use for lookups.

Methods


get_context()

@classmethod
def get_context(
name: string,
value: any,
attrs: dict
) - > dict

Constructs the template context for rendering the widget, including the lookup URL and CSS classes required for the JavaScript raw ID interface.

Parameters

NameTypeDescription
namestringThe name of the form field.
valueanyThe current value of the field, typically the primary key of the related object.
attrsdictAdditional HTML attributes to be added to the widget's input element.

Returns

TypeDescription
dictA dictionary containing template variables such as related_url, link_label, and widget attributes.

base_url_parameters()

@classmethod
def base_url_parameters() - > dict

Generates the initial set of query parameters for the lookup URL based on the relationship's limit_choices_to attribute.

Returns

TypeDescription
dictA dictionary of query parameters used to filter the related model's changelist.

url_parameters()

@classmethod
def url_parameters() - > dict

Builds the complete set of query parameters for the lookup popup, including the specific field the relationship targets.

Returns

TypeDescription
dictA dictionary containing both base filter parameters and the target field variable.

label_and_url_for_value()

@classmethod
def label_and_url_for_value(
value: any
) - > tuple

Retrieves a descriptive text label and the admin change URL for a specific related object ID.

Parameters

NameTypeDescription
valueanyThe identifier of the related object used to perform the database lookup.

Returns

TypeDescription
tupleA tuple containing a truncated string representation of the object and its admin change URL, or empty strings if not found.