Django foreign key specify field values - group by groups

Is it possible to make a foreign key value from specific values in the field from the table?

For EXA: To create a new record, HR and POC have foreign key via the employee table, but HR can bring only "HR" (hr1, hr2) values from "name" field - group by group_name field. Also POC table: all values grouped by "POC" (poc1 and poc2)

Text

TNX alot

I found a solution. In Django model should add "limit_choices_to" into the field.

limit_choices_to={"group_code": "2"} group_code: group by this field from Group model, "2" the value for lookup

employee_type = models.ForeignKey(
        Group,
        related_name="employee_type",
        verbose_name="Employee Type",
        on_delete=models.CASCADE,
        limit_choices_to={"group_code": "2"},
        blank=False,
        default="None",
    )
Back to Top