ForeignKeyRawIdWidget
A Widget for displaying ForeignKeys in the "raw_id" interface rather than in a < select > box.
Attributes
| Attribute | Type | Description |
|---|---|---|
| template_name | string = admin/widgets/foreign_key_raw_id.html | Path 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
| Name | Type | Description |
|---|---|---|
| rel | object | The 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. |
| attrs | dict = None | Optional dictionary of HTML attributes to be set on the widget. |
| using | str = None | The 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
| Name | Type | Description |
|---|---|---|
| name | string | The name of the form field. |
| value | any | The current value of the field, typically the primary key of the related object. |
| attrs | dict | Additional HTML attributes to be added to the widget's input element. |
Returns
| Type | Description |
|---|---|
dict | A 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
| Type | Description |
|---|---|
dict | A 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
| Type | Description |
|---|---|
dict | A 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
| Name | Type | Description |
|---|---|---|
| value | any | The identifier of the related object used to perform the database lookup. |
Returns
| Type | Description |
|---|---|
tuple | A tuple containing a truncated string representation of the object and its admin change URL, or empty strings if not found. |