简体   繁体   English

Django:从表单更新对象

[英]Django: Update an object from form

I'm trying to make my view to update an existing object, filling its attrs with form data. 我试图使我的看法更新现有对象,用表单数据填充其attrs。 I found the syntaxis for this: 我发现语法是这样的:

Auxi.objects.filter(id=auxi).update(name='The new name', age=23, role='The new role')

(Assuming that auxi is the object id value that the view is receiving) (假设auxi是视图正在接收的对象id值)

But the problem is that it will become too extensive for my object since it has 52 attrs and I just want to tell Django to replace all the values from my object with the data from form. 但是问题在于,由于它具有52个attrs,因此它对于我的对象来说将变得太广泛了,我只想告诉Django将来自对象的所有值替换为来自表单的数据。

I tried: 我试过了:

form = AuxiForm(request.POST)
item = form.save(commit=False)
Auxi.objects.filter(id=auxi).update(form)

and also 并且

Auxi.objects.filter(id=auxi).update(item)

But it's saying update() takes 1 positional argument but 2 were given . 但这是说update() takes 1 positional argument but 2 were given Could someone help me please? 有人可以帮我吗? Sorry if it's a duplicate post, I already tried to find it somewhere but I couldn't. 抱歉,如果它是重复的帖子,我已经尝试在某处找到它,但是找不到。

This is how i update my instances: 这就是我更新实例的方式:

obj, created = Auxi.objects.update_or_create(
    id=auxi,
    defaults={
        name='The new name', 
        age=23, 
        role='The new role'}
)
obj.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM