我正在打印下面的列表。
manifestData = form2.cleaned_data
print(manifestData)
输出的结果是:
[{'ProductCode': <Product: 1>, 'UnitQty': u'11', 'Price': u'11.00', 'Amount': u'121', 'DescriptionOfGoods': u'Washington Extra Fancy', 'Type': u'Cases', u'id': None, u'DELETE': False}, {'ProductCode': <Product: 2>, 'UnitQty': u'1', 'Price': u'12.00', 'Amount': u'12', 'DescriptionOfGoods': u'SUNKIST ORANGES', 'Type': u'Cases', u'id': None, u'DELETE': False}]
然后在我的视图中,我有下面的内容: 我试图通过会话数据传递这个变量,但我必须使ProductCode JSON可序列化......。
product = manifestData.pop('ProductCode')
当上面一行运行时,我得到了一个KeyError,上面写着 "Exception Value. ProductCode": ProductCode"。 完整的回溯如下。 有什么办法可以解决这个问题吗?
回溯
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/create/quote/
Django Version: 1.11.29
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Poseidon',
'crispy_forms']
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 "/Library/Python/2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/Users/andrews/Desktop/WBU2/Poseidon/views.py" in QuoteView
639. product = data.pop('ProductCode')
Exception Type: KeyError at /create/quote/
Exception Value: 'ProductCode'
更新
我摆脱了按键错误(只是在我的代码中打错了)--但现在我得到了一个TypeError,它说 integer is required
视图.py
def QuoteView(request):
QuoteManifestForm= modelformset_factory(QuoteManifest, QManifestForm, can_delete = True)
if request.method == "POST":
form2 = QuoteManifestForm(request.POST)
manifestData = form2.cleaned_data
for item in manifestData:
product = manifestData.pop('ProductCode') #TypeError comes here
print(product)
....
模型.py
class QuoteManifest(models.Model):
quote = models.ForeignKey(Quote, null=True, on_delete=models.CASCADE)
ProductCode = models.ForeignKey(Product, null=True)
DescriptionOfGoods = models.CharField(max_length=500, blank=True)
UnitQty = models.CharField(max_length=10, blank=True)
Type = models.CharField(max_length=50, blank=True)
Amount = models.CharField(max_length=100, blank=True)
Price = models.CharField(max_length=100, blank=True)
class Product(models.Model):
ProductCode = models.CharField(max_length=100)
HSCode = models.CharField(max_length=50, null=True)
DescriptionOfGoods = models.CharField(max_length=100)
Price = models.CharField(max_length=100)
UnitType = models.CharField(max_length=100)
def __str__(self):
return str(self.ProductCode)
表单.py
class QManifestForm(forms.ModelForm):
class Meta:
model = QuoteManifest
fields =('ProductCode', 'DescriptionOfGoods', 'UnitQty', 'Type','Amount', 'Price')
我知道它正在寻找 ProductCode
是一个整数,但我不明白为什么会这样......