如何更改 inlineformset_factory 中的模型参数?

How to change model argument in inlineformset_factory?

一切都按照这个逻辑运作。有一个 Project 模型,其中包含有关项目的一般信息。许多用户可以处理此项目(模型 Employee)并执行他们在项目工作中的部分(模型 Work)。 我使用 inlineformset_factory 创建了一个用于编辑表单的视图。

forms.py

ProjectEditFormSet = inlineformset_factory(Project, Work,
                                       form=WorkCreateForm,
                                       extra=0)

models.py

class Work(models.Model):
    """On which projects an employee has worked."""

    employee = models.ForeignKey(
        Employee, on_delete=models.CASCADE, related_name='employee_projects')
    project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='project_work')
    start_year = models.CharField('work start year', max_length=4, default=datetime.datetime.now().year)
    start_month = models.CharField('work start month', max_length=10, default=datetime.datetime.now().month)
    end_year = models.CharField('work end year', max_length=4, default=datetime.datetime.now().year)
    end_month = models.CharField('work end month', max_length=10, default=datetime.datetime.now().year)
    responsibility = models.TextField("employee work responsibility", blank=True)
    technologies = models.ManyToManyField(
        Technology, verbose_name="technologies used on the project")

class Project(models.Model):
    """Project information."""

    name = models.CharField("project name", max_length=64)
    description = models.TextField("project description")

views.py

class WorkEditView(AuthorizedMixin, UpdateView):
    """
    Edit new project instances
    """
    model = Project
    form_class = ProjectForm
    template_name = 'work_edit.html'


    def get_context_data(self, **kwargs):
        context = super(WorkEditView, self).get_context_data(**kwargs)
        context['project_form'] = ProjectEditFormSet(instance=self.object)
        return context

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        project_form = WorkFormSet(self.request.POST, instance=self.object)
        if form.is_valid() and project_form.is_valid():
            return self.form_valid(form, project_form)
        else:
            return self.form_invalid(form, project_form)


    def form_valid(self, form, project_form):
        self.object = form.save()
        project_form.instance = self.object
        project_form.save()
        return redirect('{}#experience'.format(reverse('profile', kwargs={'pk': self.kwargs['employee']})))

它现在对我有用。

编辑时,显示与项目相关的所有工作表单(参与项目的每个用户的表单)

我想得到什么结果

只显示属于正在编辑个人资料的当前用户的编辑表单。

urls.py

 path('work/<int:employee>/<int:pk>/edit/', WorkEditView.as_view(), name='work_info_edit'),

其中 <int:employee> - 当前员工; <int:pk> - 项目编号

我想我需要改变

def get_context_data(self, **kwargs):
    context = super(WorkEditView, self).get_context_data(**kwargs)
    context['project_form'] = ProjectEditFormSet(instance=self.object)  <--Smth here need change ??????

但是我找不到办法。

与任何 ModelFormSet 一样,您可以在实例化 ProjectEditFormSet 时指定一个 queryset 参数:

employee_id = self.kwargs['employee']
employee = Employee.objects.get_or_404(id=employee_id)
project_form = ProjecEditFormSet(request.POST, instance=self.object, 
    queryset=Work.objects.filter(employee=employee))