在使用Django PurchaseOrder
的通用视图将数据保存到数据库的两个表PurchaseOrderLineItems
和CreateView
时遇到问题。
我收到此错误:ValueError: not enough values to unpack (expected 2, got 1)
,但我不知道错误在代码中的何处。
我从html表(每行具有复选框)中获取数据,该表由另一个称为Masterlist
的表生成,并将保存在PurchaseOrderLineItems
中。
这是我通过搜索如何保存数据来尝试的方法。我在CreateView类中创建了一个发布函数。
class PurchaseOrderCreateView(LoginRequiredMixin, CreateView):
model = PurchaseOrder
fields = ['project_site', 'supplier']
def form_valid(self, form):
form.instance.prepare_by = self.request.user
return super().form_valid(form)
def get_context_data(self, **kwargs):
kwargs['object_list'] = Masterlist.objects.all()
return super(PurchaseOrderCreateView, self).get_context_data(**kwargs)
def post(self, request, *args, **kwargs):
po = PurchaseOrder.objects.all()
if request.method == 'POST':
get_list = request.POST.getlist('checks[]')
for item in get_list:
get_id = Masterlist.objects.filter(item[0])
save_to_pol = PurchaseOrderLineItems(item_no=get_id.item_no, description=get_id.description, dimension=get_id.dimension, unit=get_id.unit, quantity=get_id.quantity, cost=get_id.cost, total=get_id.cost * get_id.quantity, po_fk=pk)
save_to_pol.save()
return super().post(request)
错误回溯:
Environment:
Request Method: POST
Request URL: http://localhost:8000/lastmile/purchaseorder/new/
Django Version: 2.1.4
Python Version: 3.7.1
Installed Applications:
['users.apps.UsersConfig',
'lastmile.apps.LastmileConfig',
'crispy_forms',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/contrib/auth/mixins.py" in dispatch
52. return super().dispatch(request, *args, **kwargs)
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/views/generic/base.py" in dispatch
88. return handler(request, *args, **kwargs)
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/Application/ERP/lastmile/views.py" in post
57. get_id = Masterlist.objects.filter(item[0])
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/db/models/query.py" in filter
844. return self._filter_or_exclude(False, *args, **kwargs)
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/db/models/query.py" in _filter_or_exclude
862. clone.query.add_q(Q(*args, **kwargs))
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/db/models/sql/query.py" in add_q
1263. clause, _ = self._add_q(q_object, self.used_aliases)
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/db/models/sql/query.py" in _add_q
1287. split_subq=split_subq,
File "/Users/workstationthreesoftwaredeveloper/JXMTSI/Projects/Office/Application/Web-Application/Django/environ/lib/python3.7/site-packages/django/db/models/sql/query.py" in build_filter
1161. arg, value = filter_expr
Exception Type: ValueError at /lastmile/purchaseorder/new/
Exception Value: not enough values to unpack (expected 2, got 1)
我想要的是,当用户填写PurchaseOrder
的字段,然后检查html表(由Masterlist
生成)中的项目并单击按钮时,它将保存选中的PurchaseOrderLineItems