简体   繁体   English

Django管理员从相关模型添加表单显示对象字段

[英]Django admin add form show object fields from related model

I have following model: 我有以下模型:

class Company(models.Model):
    company_name = models.CharField(verbose_name="Company", max_length=200)


class Department(models.Model):
    name = models.CharField(verbose_name="Department", max_length=255)
    company = models.ForeignKey(Company, on_delete=models.CASCADE)

class Employee(models.Model):

    external_id = models.CharField(max_length=50, null=True, blank=True)
    name = models.CharField(max_length=100)  
    department = models.ForeignKey(Department, on_delete=models.CASCADE)

I am trying to have my Add employee admin form have besides Department, related selection for Company. 我正在尝试让我的“添加员工管理”表单除了部门,公司的相关选择之外。

So when i select Company i get choice of its departments. 因此,当我选择公司时,便可以选择其部门。

Currently i get choice of all the departments. 目前,我可以选择所有部门。

App is heavily admin focused so it would be good to have this functionality. 该应用程序主要是针对管理员的,因此拥有此功能会很好。

You can try using django autocomplete light. 您可以尝试使用Django自动填充指示灯。 Here are the docs . 这是文档

This will give you the functionality to display options in the child filter based on the selection in parent filter with the help of its forward paramater. 这将使您能够借助其forward参数根据父过滤器中的选择在子过滤器中显示选项。

Create a form class with custom company field and then forward the field to department filter as: 使用自定义公司字段创建表单类,然后将字段转发为部门过滤器,如下所示:

class EmployeeForm(forms.ModelForm):
    company = forms.ModelChoiceField(queryset=Company.objects.all(), 
                                 widget=autocomplete.ModelSelect2(url='your_company_auto_url'),
                                 required=False)
    department = forms.ModelChoiceField(queryset=Department.objects.all(),
                                    widget=autocomplete.ModelSelect2(url='your_department_auto_url',
                                                                     forward=['company']))

    class Meta:
        model = Employee
        fields = '__all__'

Department View: 部门视图:

class DepartmentAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        if not self.request.user.is_authenticated():
            return Department.objects.none()

        qs = Department.objects.all()

        company = self.forwarded.get('company', None)

        if company:
            qs = qs.filter(company=company)

        if self.q:
            qs = qs.filter(name__istartswith=self.q)
        return qs

This will forward the selected company id and it can then be used in the autocomplete view to filter the department queryset. 这将转发选定的company ID,然后可在自动完成视图中使用它来过滤部门查询集。 Please refer to the documentation to create autocomplete views to be used for the fields in the form. 请参考文档以创建自动完成视图以用于表单中的字段。

Hope it helps. 希望能帮助到你。

IMO,也许您需要将“ Klijent”更改为“ Company”,我想知道“ Klijent”的用途是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM