Skip to main content

InlineForeignKeyField

A basic integer field that deals with validating the given value to a given parent instance in an inline.

Attributes

AttributeTypeDescription
widget[Widget](../widgets/widget.md?sid=django_forms_widgets_widget) = HiddenInputThe form widget class used to render this field, defaulting to a hidden input to store the parent relationship without user interaction.
default_error_messagesdict = {"invalid_choice": "The inline value did not match the parent instance."}A dictionary containing the default error message for when the provided inline value does not match the parent instance.

Constructor

Signature

def InlineForeignKeyField(
parent_instance: object,
*args: any,
pk_field: boolean = False,
to_field: string = None,
**kwargs: any
) - > null

Parameters

NameTypeDescription
parent_instanceobjectThe parent model instance this field is associated with.
*argsanyVariable length argument list passed to the parent Field class.
pk_fieldboolean = FalseFlag indicating if this is a primary key field.
to_fieldstring = NoneThe specific field on the parent instance to link against.
**kwargsanyArbitrary keyword arguments passed to the parent Field class.

Signature

def InlineForeignKeyField(
parent_instance: Model instance,
pk_field: boolean,
to_field: string
) - > None

Parameters

NameTypeDescription
parent_instanceModel instanceThe parent model instance that this inline field must validate against.
pk_fieldbooleanA flag indicating if this field represents the primary key of the model.
to_fieldstringThe specific attribute name on the parent instance to use for validation instead of the primary key.

Methods


clean()

@classmethod
def clean(
value: any
) - > Model instance

Validates that the submitted value matches the parent instance's identifier and returns the parent instance if successful.

Parameters

NameTypeDescription
valueanyThe raw input value to be validated against the parent instance's identity.

Returns

TypeDescription
Model instanceThe validated parent model instance or None if the field is a primary key field and the value is empty.

has_changed()

@classmethod
def has_changed(
initial: any,
data: any
) - > boolean

Determines if the field value has changed, always returning False to prevent the inline foreign key from triggering change detection.

Parameters

NameTypeDescription
initialanyThe initial value of the field.
dataanyThe submitted data to compare against the initial value.

Returns

TypeDescription
booleanAlways returns False to indicate the field is considered unchanged.