首页 > 解决方案 > 来自两个不同表的依赖链下拉菜单的 Django 自动完成轻值错误

问题描述

我一直在阅读 DAL 教程,现在尝试将它实施到我的项目中,但不断得到

“ValueError:要解压的值太多(预期 2)[25/May/2020 18:42:17] “GET /wellsurfer/ajax/load_casing_weights?forward=%7B%7D HTTP/1.1”500 17744 “表格看起来像如下,如您所见,我无法制作表单来过滤结果,因此填充权重菜单 在此处输入图像描述

表格.py

class NewCasingDesignForm(forms.ModelForm):
od = forms.ModelChoiceField(queryset=CasingCatalogue.objects.order_by().distinct().values_list('size', flat=True))

class Meta:
    model = CasingDesign
    fields = (
        'well', 'od', 'weight', 'pipeyield', 'connection','inner_diameter', 'set_depth', 'top_depth', 'type_csg_lnr',)
    help_texts = {
        'well': 'Select a well',
        'od': 'unit, inches',
        'weight': 'unit, lbs',
        'pipeyield': 'unit, psi',
        'connection': 'Type',
        'inner_diameter': 'unit, inches',
        'set_depth': 'What depth (feet) the pipe is set (shoe depth)',
        'top_depth': '0 feet to indicate casing or a depth greater than 0 for liner',
        'type_csg_lnr': 'Confirm if string is a casing or liner',

    }

    widgets = {
        'well': forms.Select(attrs={
            'class': 'form-control',
        }),
        'od': forms.Select(attrs={
            'class': 'form-control',
        }),
        'weight': autocomplete.ListSelect2(url='wellsurfer:ajax_load_casing_weight', forward=['od']),
        'inner_diameter': forms.NumberInput(attrs={
            'class': 'form-control',
            'placeholder': '8.835'
        }),
        'set_depth': forms.NumberInput(attrs={
            'class': 'form-control',
            'placeholder': '5000'
        }),
        'top_depth': forms.NumberInput(attrs={
            'class': 'form-control',
            'placeholder': '0'
        }),
        'type_csg_lnr': forms.Select(attrs={
            'class': 'form-control',
        }),
    }

视图.py

def new_casing_design(request):
if request.method == 'POST':
    form = NewCasingDesignForm(request.POST)
    if form.is_valid():
        newcasingdesign = form.save(commit=False)
        newcasingdesign.created_by = request.user
        newcasingdesign.created_at = timezone.now()
        newcasingdesign.save()
        return redirect('wellsurfer:index')
else:
    form = NewCasingDesignForm()
return render(request, 'wellsurfer/create_new_casing_design.html', {'create_casing': form})


def load_casing_weight(request):
    od = request.GET.get('od')
    weight = CasingCatalogue.objects.filter(bodyod=od).distinct().values_list('nomweight', flat=True)
    return weight

网址.py

from django.urls import path
from . import views

app_name = 'wellsurfer'
urlpatterns = [
path('', views.index, name='index'),  # empty string # represents root of the app
path('wells/<name>/', views.well_details, name='well_details'),
path('wells/<name>/<run>/<run_id>', views.run_record, name='run_record'),
path('wells/<well_name>/<plan_or_survey_name>', views.plan_or_survey_view, name='plan_or_survey_view'),
path('search', views.search, name='search'),
path('new-operator/', views.new_operator, name='new_operator'),
path('new-asset/', views.new_asset, name='new_asset'),
path('new-pad/', views.new_pad, name='new_pad'),
path('new-well/', views.new_well, name='new_well'),
path('new-well-plan/', views.new_well_plan, name='new_well_plan'),
path('new-casing-design/', views.new_casing_design, name='new_casing_design'),
path('ajax/load_casing_weights', views.load_casing_weight, name='ajax_load_casing_weight'),

]

标签: djangodjango-modelsdjango-formsdjango-viewsdjango-templates

解决方案


如果您想要 JSON 响应而不是,您应该返回JsonResponse

return weight

符合以下要求的东西

return JsonResponse(list(weight))

推荐阅读