如何在CreateView中覆盖modelformset的字段值?

时间:2019-07-29 07:15:07

标签: python django django-forms django-views

我对同一模型的modelformset使用不同的qwerysets。我在forms.py

中定义的用于对来自查询集的值进行分组的第一个过滤器
class SkillCreateFrameworkForm(SkillBaseCreateForm):
    def __init__(self, *args, **kwargs):
        super(SkillCreateFrameworkForm, self).__init__(*args, **kwargs)
        self.fields['technology'].queryset = Technology.objects.filter(group__name="Framework")

class SkillCreatePLanguageForm(SkillBaseCreateForm):
    def __init__(self, *args, **kwargs):
        super(SkillCreatePLanguageForm, self).__init__(*args, **kwargs)
        self.fields['technology'].queryset = Technology.objects.filter(group__name="Programming language")


SkillFrameworkFormSet = modelformset_factory(Skill, form=SkillCreateFrameworkForm, extra=get_extra_framework(), can_delete=False)

SkillPLanguageFormSet = modelformset_factory(Skill, form=SkillCreatePLanguageForm, extra=get_extra_language(), can_delete=False)

下一步是过滤出已绑定到正在编辑的用户的值。为此,在get_context_data中,我获得了用户已经拥有的所有技术的列表。

 employee_current_technology = Technology.objects.filter(skill__employee__id=self.kwargs['pk'])

get_context_data

def get_context_data(self, **kwargs):
    context = super(SkillTestCreateView, self).get_context_data(**kwargs)
    employee_current_technology = Technology.objects.filter(skill__employee__id=self.kwargs['pk'])
    context['formset_framework'] = SkillFrameworkFormSet()
    context['formset_planguage'] = SkillPLanguageFormSet()
    context['tech_group'] = Techgroup.objects.all()
    return context

我现在如何对不包含用户已经拥有的技术的context ['formset_framework']应用过滤器? 像

context['formset_framework'] = SkillFrameworkFormSet().field['technology'] not in employee_current_technology <-- ???

0 个答案:

没有答案