I would like to add dependent fields to my django admin panel its like when you select one company it must be display all roles relative with this company in admin, but my admin looks like that:
it not displaying company and roles
i can say my all imports are correct, already add selec2/ for urls, and installed 8.1.2 version of django-select2.
whats my mistake ?
forms.py
class StaffAdminForm(forms.ModelForm):
company = forms.ModelChoiceField(
queryset = Company.objects.all(),
label = "Company",
widget=ModelSelect2Widget(
model=Company,
search_fields=['name__icontains'],
)
)
role = forms.ModelChoiceField(
queryset = Role.objects.all(),
label= "Role",
widget=ModelSelect2Widget(
model=Role,
search_fields=['name__icontains'],
dependent_fields={'company': 'company'},
max_results=500,
)
)
admin.py
from django.contrib import admin
from .forms import StaffAdminForm
from .models import Staff
@admin.register(Staff)
class StaffAdmin(admin.ModelAdmin):
form = StaffAdminForm
list_display = (
"id",
"fio",
"branch",
"company",
"role",
)
list_filter = ("company",)
list_select_related = (
"company",
"role",
"branch",
)
models.py
class Staff(models.Model):
user = models.ForeignKey(
"users.User",
on_delete=models.CASCADE,
related_name="staffs",
related_query_name="staff",
)
company = models.ForeignKey(
"companies.Company",
on_delete=models.CASCADE,
related_name="staffs",
related_query_name="staff",
)
branch = models.ForeignKey(
"companies.Branch",
on_delete=models.CASCADE,
related_name="staffs",
related_query_name="staff",
null=True,
blank=True,
help_text=_("Storing branch staff is related to for upcoming operation"),
)
role = models.ForeignKey(
"rbac.Role",
on_delete=models.CASCADE,
related_name="staffs",
related_query_name="staff",
)
fio = models.CharField(
_("FIO"),
blank=True,
max_length=128,
help_text=_("Full name written in official document"),
)
And already tried another variants like gpt and Google but it not helped.