我正在寻找一种干净,干燥的方式来使用ModelForm和AJAX更新对象。
这是我所拥有的:
ERROR [app-router] TypeError: rolePerms.get is not a function
at Utils.userRoleAuthorized (utils.ts:29)
at NewCustomer.canActivate (newCustomer.ts:42)
at iterate (index.ts:7)
at processActivatable (index.ts:7)
at CanActivateNextStep.run (index.ts:7)
at next (index.ts:7)
at Object.<anonymous> (shell.ts:58)
at step (vendor-bundle.js:4344)
at Object.next (vendor-bundle.js:4325)
at vendor-bundle.js:4318
at Promise._execute (vendor-bundle.js:824)
at Promise._resolveFromExecutor (vendor-bundle.js:2601)
at new Promise (vendor-bundle.js:2197)
at __awaiter (vendor-bundle.js:4314)
at Object.run (shell.ts:45)
at next (index.ts:7)
我遇到的问题是def update_entry(request):
if not request.is_ajax() or not request.method == "POST":
pass
form_data = dict(request.POST)
instance = form_data.pop('id')
form_data['date'] = datetime.datetime(year=int(form_data.pop('year')[0]), month=int(form_data.pop('month')[0]), day=int(form_data.pop('date')[0]))
form = EntryForm(form_data, instance=instance)
if form.is_valid():
form.save()
return JsonResponse({'message': "success"})
对象是不可变的,因此我不能实际使用request.POST
而不将表单更改为dict对象或其他对象。如果这样做,最终会将所有字符串值更改为包含字符串值的列表。我还需要创建日期值。
我是在正确的轨道上吗,还是有更多的“ Django”方法来解决这个问题?