简体   繁体   English

向 inlineformset_factory 添加额外参数

[英]Add extra parameter to inlineformset_factory

After a few hours of searching I must admit that I am defeated.经过几个小时的搜索,我必须承认我失败了。

I have read the Django docs but I really can't find a solution to my problem.我已经阅读了 Django 文档,但我真的找不到解决我的问题的方法。

Consider the following line of code:考虑以下代码行:

EmploymentFormSet = inlineformset_factory(Profile, Employment, form=EmploymentForm, extra=3)

This code lives in a classbased view which inherits from UpdateView and furthermore in the method get_context_data(self, *args, **kwargs):这段代码存在于一个基于类的视图中,该视图继承自UpdateView以及get_context_data(self, *args, **kwargs):

This is pretty straight forward as the inlineformset_factory creates an EmploymentFormSet.这是非常简单的,因为 inlineformset_factory 创建了一个 EmploymentFormSet。

Now consider this现在考虑这个

queryset = Employment.objects.filter(profile__pk=self.kwargs['pk']).values()
context['emp_formset'] = EmploymentFormSet(prefix='emp_form', initial=queryset, auto_id=True)

I thought by supplying initial=queryset , which only applies to unbound instances IIRC, it would populate my formset with as many as the queryset would contain.我认为通过提供仅适用于未绑定实例 IIRC 的initial=queryset ,它会用查询集包含的尽可能多的内容填充我的表单集。

So the queryset will in my case return 4 Employments but when using the extra parameter, the formset I'm constructing are only filled with as many as this parameter defines, in my example only 3 since I defined only 3 extras.因此,在我的情况下,查询集将返回 4 个Employments但是当使用extra参数时,我构建的表单集只填充了该参数定义的数量,在我的示例中只有 3 个,因为我只定义了 3 个额外的。 Incrementing the extra will populate the forms incrementally.增加额外的将逐渐填充表单。

I've tried subclassing the BaseInlineFormSet but I haven't really broken through the wall.我已经尝试对BaseInlineFormSet子类化,但我还没有真正突破这堵墙。

My question is how would I go about to populate the formset with as many forms as the queryset contains, I'm not really out for an exact solution but more of a pointer in the right the direction!我的问题是我将如何使用查询集包含的尽可能多的表单来填充表单集,我并不是真的想要一个确切的解决方案,而是更多指向正确方向的指针! :) :)

Thanks!谢谢!

I solved it by constructing this method我通过构造这个方法解决了它

def set_extra_forms(extra_forms, **kwargs):
    EmploymentFormSet = inlineformset_factory(Profile, Employment, form=EmploymentForm, extra=extra_forms)
    return EmploymentFormSet(**kwargs)

Now I do believe this is the way to go but I have to refactor the code in order to make it more dynamic right now its connected to one class and one class only but it works like a charm.现在我确实相信这是要走的路,但我必须重构代码以使其更具动态性,现在它只连接到一个类和一个类,但它的作用就像一个魅力。

Define the formset inside a view在视图中定义表单集

def employment_view(request, pk=None):
  #Define extra forms variable
  extra_forms = Employment.objects.filter(profile__pk=self.kwargs['pk']).count()
  #Define formset inside the view function
  EmploymentFormSet = inlineformset_factory(Profile, Employment, form=EmploymentForm, extra=extra_forms)
  #Use this in context
  context['emp_formset'] = EmploymentFormSet()

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

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