简体   繁体   中英

Django: Update an object from form

I'm trying to make my view to update an existing object, filling its attrs with form data. 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)

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.

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 . 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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