我正在尝试使用 POST 数据查询字典从 django 模型中保存一个新对象。这是 PISTON 处理程序的一部分。我已经在许多示例中看到了这一点,但我就是无法让它发挥作用。
这是我的代码:
class FestHandler(BaseHandler):
model = Deal
def create(self, request):
"""
Creates a new fest.
"""
attrs = self.flatten_dict(request.POST)
postcopy = request.POST.copy()
if self.exists(**attrs):
return rc.DUPLICATE_ENTRY
else:
loc = Location.objects.get(pk=attrs['location'])
postcopy['location'] = loc
fest = Fest(postcopy)
fest.save()
return fest
这是我每次收到的错误:
Exception was: int() argument must be a string or a number, not 'QueryDict'
我意识到错误的含义,所以基本上我在问如何通过传入整个 POST 字典来保存新的“Fest”,而不必每次都像这样手动输入键:
loc = Location.objects.get(pk=attrs['location'])
fest = Fest(
location=loc,
name=attrs['name'],
description=attrs['description'],
details=attrs['details'],
)
谢谢你的帮助!