Many to one
Purpose: displaying the number of records in the related model, by foreign key
Field type in the SQL table: the field is not created, migration will offer to create an index in the opposite model
Additional parameters:
related_model
Purpose: a mandatory parameter indicating the model that has an enum field with a foreign key that is the name of the current model
Possible values: model name, for example ‘Comments’, 'Clients'
name_field
Purpose: if the related model does not have a field called 'name', then you must explicitly specify a named field
Possible values: text, for example 'title'
Default: ‘name’
display_count
Purpose: if true, then shows the number of records in the general table corresponding foreign key, if false then list of record names
Possible values: true, false
Default: true
allow_sorting
Purpose: allows you to sort the table in the admin panel by this field, if there is a large amount of data, it is better to disable it
Possible values: true, false
Default: true
Examples:
['Comments', 'many_to_one', 'comments', ['related_model' => 'Comments']]
['Orders', 'many_to_one', 'orders', ['related_model' => 'Orders', 'name_field' => 'number']]
Previous section
Parent