【问题标题】:Django - redefine Form Select using pk URLDjango - 使用 pk URL 重新定义表单选择
【发布时间】:2020-09-28 12:33:21
【问题描述】:

我需要使用 URL 发送的 pk 设置输入(选择)值。

在表单中使用__init__,几乎可以得到答案,但是__init__方法执行了两次并清除了我的值。

表格:

class CrearDelitoForm(forms.ModelForm):
    class Meta:
        model = Delito
        exclude = ()

    def __init__(self, numero_pk = None, *args, **kwargs):
        super(CrearDelitoForm, self).__init__(*args, **kwargs)
        self.fields["imputado"].queryset = Imputado.objects.filter(numero_id = numero_pk)

DelitoFormset = inlineformset_factory(
    Expediente,
    Delito,
    form=CrearDelitoForm,    
    extra=1,
    can_delete=True,
    fields=('imputado', 'delito', 'categoria'),
    } 
)

观看次数:

class CrearDelito(CreateView):
    model = Delito
    form_class = CrearDelitoForm
    template_name = 'crear_delito.html'

    def get_context_data(self,**kwargs):
        context = super().get_context_data(**kwargs)
        context['formset'] = DelitoFormset()        
        context['expedientes'] = Expediente.objects.filter(id = self.kwargs['pk'])
        return context

    def get_form_kwargs(self, **kwargs):
        kwargs['numero_pk'] = self.kwargs['pk']
        return kwargs

** 系统检查未发现任何问题(0 静音)。 2020 年 6 月 8 日 - 11:33:57 Django 2.2.12 版,使用设置“red.settings” 在http://127.0.0.1:8000/ 启动开发服务器 使用 CTRL-BREAK 退出服务器。 , ]> **

我认为问题出在 context = super().get_context_data(**kwargs) 但不知道为什么。

【问题讨论】:

    标签: django forms url select init


    【解决方案1】:

    CreateView 创建一个表单,即您在 form_class 中指定的表单。它还会将request.POST 和request.FILES 传递给表单,因此您不必自己构建一个表单集,而是让CreateView 来做。

    您需要将字典传递给表单集的form_kwargs 到form_kwargs,因此需要将get_form_kwargs 更改为:

    class CrearDelito(CreateView):
        model = Delito
        form_class = DelitoFormset
        template_name = 'crear_delito.html'
    
        def get_context_data(self,**kwargs):
            context = super().get_context_data(**kwargs)
            context['expedientes'] = Expediente.objects.filter(id=self.kwargs['pk'])
            context['formset'] = context['form']
            return context
    
        def get_form_kwargs(self, **kwargs):
            form_kwargs = super().get_form_kwargs(**kwargs)
            form_kwargs['form_kwargs'] = {'numero_pk': self.kwargs['pk']}
            form_kwargs['instance'] = Expediente.objects.get(id=self.kwargs['pk'])
            return form_kwargs
    
        def get_success_url(self):
            return reverse('repositorio:crear_victima', args=[request.POST.get('numero_id')])

    【讨论】:

    • 它有效,但仍然没有显示 imputados(被告)的选择。我有 3 个模型:Delito(犯罪)、imputados(被告)和 expediente(文件)。加载文件信息后,系统需要imputados(被告)。在那之后,我需要每个被告获得所有的罪行。我使用表单集和动态创建表单来做到这一点。所以模板接收 expediente pk(文件 pk)。 Whit this file pk 我只需要显示该文件中的 imputados(accused)。使用此解决方案,它缺少以 imputados(被告)的形式开发的查询集。我是 Django 的新手,所以为我的错误道歉..
    • @MarceIaco:通常是通过['form_kwargs'] = {'numero_pk': self.kwargs['pk']} 传递的。但是我犯了一个小错误:它应该返回form_kwargs,你可以试试编辑后的版本吗?
    • 起首!!其作品!我真诚地感谢和钦佩您的知识。
    • 我有一个新问题。最后形成结果无效......“选择一个有效的选择。该选择不是可用的选择之一”我认为这是关于 imputado pk (被告)。但是如果我比较以前的工作脚本,它会显示相同的值... ('delito_set-TOTAL_FORMS', '1')('delito_set-INITIAL_FORMS', '0')('delito_set-MIN_NUM_FORMS', ' 0')('delito_set-MAX_NUM_FORMS', '1000')('delito_set-0-id', '')('delito_set-0-numero', '')('delito_set-0-imputado', '12' )('delito_set-0-delito', '43')('delito_set-0-categoria', 'Principal')('delito_set-0-DELETE', '') .
    • 工作脚本结果:('delito_set-TOTAL_FORMS', '1')('delito_set-INITIAL_FORMS', '0')('delito_set-MIN_NUM_FORMS', '0')(' delito_set-MAX_NUM_FORMS', '1000')('delito_set-0-id', '')('delito_set-0-numero', '')('delito_set-0-imputado', '12')('delito_set- 0-delito', '43')('delito_set-0-categoria', 'Principal')('delito_set-0-DELETE', '')
    猜你喜欢
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 2015-07-06
    相关资源
    最近更新 更多