Skip to main content

model_to_dict

Return a dict containing the data in instance suitable for passing as a Form's initial keyword argument.

def model_to_dict(
instance: Model,
fields: list = null,
exclude: list = null
) - > dict

Return a dict containing the data in instance suitable for passing as a Form's initial keyword argument.

Parameters

NameTypeDescription
instanceModelThe Django model instance from which to extract data.
fieldslist = nullAn optional list of field names. If provided, return only the named.
excludelist = nullAn optional list of field names. If provided, exclude the named from the returned dict, even if they are listed in the fields argument.

Returns

TypeDescription
dictA dictionary mapping field names to their corresponding values extracted from the model instance.