Django 3.x Python 3.x表单-';非类型';对象没有属性'_预回迁相关查找';

Django 3.x Python 3.x表单-';非类型';对象没有属性'_预回迁相关查找';,python,django,django-forms,Python,Django,Django Forms,我在下面有导致错误的表格 class ErkbRamsis(forms.Form): def __init__(self, *args, **kwargs): self.transfer_id = kwargs.pop('transfer_id') transfer = Transfer.objects.get(pk = self.transfer_id) if 'coach_id' in kwargs: self.

我在下面有导致错误的表格

class ErkbRamsis(forms.Form):
    def __init__(self, *args, **kwargs):
        self.transfer_id = kwargs.pop('transfer_id')
        transfer = Transfer.objects.get(pk = self.transfer_id)
        if 'coach_id' in kwargs:
            self.coach_id = kwargs.pop('coach_id')
            leg = Leg.objects.get(pk = self.coach_id)
        else:
            leg = Leg.objects.filter(service = transfer.service).first()

        super(ErkbRamsis, self).__init__(*args, **kwargs)

        self.fields['transfer_clients'] = forms.ModelMultipleChoiceField(
            queryset=Client.objects.filter(related_pnr = transfer.service.related_pnr),
            widget=forms.SelectMultiple(),
            required=False,
        )
        self.fields['coach'] = forms.ModelChoiceField(
            queryset=Leg.objects.filter(service = transfer.service),
            widget=forms.Select(),
            empty_label=None,
            initial=leg,                        #### for non assigned legs cause error
            required=False,
        )
        self.fields['coach_clients'] = forms.ModelMultipleChoiceField(
            queryset = leg.clients,             #### for non assigned legs cause error
            widget=forms.SelectMultiple(),
            required=False,
        )

    def clean(self, *args, **kwargs):
        transfer_clients = self.cleaned_data.get('transfer_clients')
        coach = self.cleaned_data.get('coach')
        coach_clients = self.cleaned_data.get('coach_clients')
        return super(ErkbRamsis, self).clean(*args, **kwargs)
场景:

Internal Server Error: /traffic/transfers/
Traceback (most recent call last):
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view      
    return view_func(request, *args, **kwargs)
  File "G:\WorkPlace\Django\wing_buzz\main\views.py", line 464, in transfers_assign
    return render(request, 'transfers_assign.html', context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 992, in render
    return render_value_in_context(output, context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 971, in render_value_in_context     
    value = str(value)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\utils\html.py", line 373, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\boundfield.py", line 33, in __str__
    return self.as_widget()
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\boundfield.py", line 92, in as_widget
    return widget.render(
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 241, in render
    context = self.get_context(name, value, attrs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 678, in get_context
    context = super().get_context(name, value, attrs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 639, in get_context
    context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 587, in optgroups
    for index, (option_value, option_label) in enumerate(self.choices):
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\models.py", line 1138, in __iter__
    if not queryset._prefetch_related_lookups:
AttributeError: 'NoneType' object has no attribute '_prefetch_related_lookups'
我有4个模型
Transfer
Service
Leg
Client
,表单将
Client
对象按客户端附加到
Leg
。添加(
Client
)。当
传输
服务
没有创建并附加到
服务
分支
对象时,表单在呈现时会导致错误。导致错误的行由注释标记

编辑:

Internal Server Error: /traffic/transfers/
Traceback (most recent call last):
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view      
    return view_func(request, *args, **kwargs)
  File "G:\WorkPlace\Django\wing_buzz\main\views.py", line 464, in transfers_assign
    return render(request, 'transfers_assign.html', context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 992, in render
    return render_value_in_context(output, context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 971, in render_value_in_context     
    value = str(value)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\utils\html.py", line 373, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\boundfield.py", line 33, in __str__
    return self.as_widget()
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\boundfield.py", line 92, in as_widget
    return widget.render(
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 241, in render
    context = self.get_context(name, value, attrs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 678, in get_context
    context = super().get_context(name, value, attrs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 639, in get_context
    context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 587, in optgroups
    for index, (option_value, option_label) in enumerate(self.choices):
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\models.py", line 1138, in __iter__
    if not queryset._prefetch_related_lookups:
AttributeError: 'NoneType' object has no attribute '_prefetch_related_lookups'
回溯:

Internal Server Error: /traffic/transfers/
Traceback (most recent call last):
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view      
    return view_func(request, *args, **kwargs)
  File "G:\WorkPlace\Django\wing_buzz\main\views.py", line 464, in transfers_assign
    return render(request, 'transfers_assign.html', context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 992, in render
    return render_value_in_context(output, context)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\template\base.py", line 971, in render_value_in_context     
    value = str(value)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\utils\html.py", line 373, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\boundfield.py", line 33, in __str__
    return self.as_widget()
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\boundfield.py", line 92, in as_widget
    return widget.render(
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 241, in render
    context = self.get_context(name, value, attrs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 678, in get_context
    context = super().get_context(name, value, attrs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 639, in get_context
    context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\widgets.py", line 587, in optgroups
    for index, (option_value, option_label) in enumerate(self.choices):
  File "G:\WorkPlace\Django\wing_buzz\venv\lib\site-packages\django\forms\models.py", line 1138, in __iter__
    if not queryset._prefetch_related_lookups:
AttributeError: 'NoneType' object has no attribute '_prefetch_related_lookups'
内部服务器错误:/traffic/transfers/
回溯(最近一次呼叫最后一次):
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\core\handlers\exception.py”,第34行,在内部
响应=获取响应(请求)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\core\handlers\base.py”,第115行,在get响应中
response=self.process\u异常\u由\u中间件(e,请求)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\core\handlers\base.py”,第113行,在get响应中
响应=包装的回调(请求,*回调参数,**回调参数)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\contrib\auth\decorators.py”,第21行,在包装视图中
返回视图功能(请求,*args,**kwargs)
文件“G:\WorkPlace\Django\wing\u buzz\main\views.py”,第464行,在传输\u分配中
返回呈现(请求“transfers\u assign.html”,上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\shortcuts.py”,第19行,在渲染中
content=loader.render_to_string(模板名称、上下文、请求、using=using)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\loader.py”,第62行,在render\u to\u字符串中
返回template.render(上下文、请求)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\backends\Django.py”,第61行,在渲染中
返回self.template.render(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第171行,在render中
返回self.\u呈现(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第163行,在渲染中
返回self.nodelist.render(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第936行,在render中
位=节点。带注释的渲染(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第903行,在带注释的render\u中
返回self.render(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\loader\u tags.py”,第150行,在render中
返回编译的\u父对象。\u呈现(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第163行,在渲染中
返回self.nodelist.render(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第936行,在render中
位=节点。带注释的渲染(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第903行,在带注释的render\u中
返回self.render(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\loader\u tags.py”,第62行,在render中
结果=block.nodelist.render(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第936行,在render中
位=节点。带注释的渲染(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第903行,在带注释的render\u中
返回self.render(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\defaulttags.py”,第209行,在render中
nodelist.append(node.render_注释(上下文))
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第903行,在带注释的render\u中
返回self.render(上下文)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第992行,在render中
在上下文(输出,上下文)中返回渲染值
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\template\base.py”,第971行,在上下文中的render\u value\u中
value=str(值)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\utils\html.py”,第373行,在
克拉斯·斯特尔:马克·萨弗(克拉斯·斯特尔)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\forms\boundfield.py”,第33行,在__
返回self.as_小部件()
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\forms\boundfield.py”,第92行,在as\U小部件中
返回widget.render(
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\forms\widgets.py”,第241行,在render中
context=self.get\u context(名称、值、属性)
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\forms\widgets.py”,第678行,在get\U上下文中
context=super()。获取上下文(名称、值、属性)
get\U上下文中的文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\forms\widgets.py”,第639行
上下文['widget']['optgroups']=self.optgroups(名称,上下文['widget']['value'],属性)
optgroups中的文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\forms\widgets.py”,第587行
对于枚举(self.choices)中的索引(选项值、选项标签):
文件“G:\WorkPlace\Django\wing\u buzz\venv\lib\site packages\Django\forms\models.py”,第1138行,在iter中__
如果不是查询集,则预取相关查找:
AttributeError:“非类型”对象没有属性“\u预取\u相关的\u查找”

您可以共享完整回溯吗?问题已通过完整回溯编辑,完成!您可以共享您的模型吗?您应该能够使用模型表单,而无需动态构造字段,如