#33834: Compact way to in-place update of the ORM model instance
-------------------------------------+-------------------------------------
Reporter: Anton | Owner: nobody
Danilchenko |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: dev
layer (models, ORM) |
Severity: Normal | Keywords: orm
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hello,
I think that it will be useful to have such a methods to update the
instance in-place, that saves a bit of typing:
{{{
book = Book.objects.get(pk=123)
# Currently we do next
book.title = "New title"
book.year = 2022
book.save()
# NEW WAY: Set multiple fields in-place
book.set(title="New title", year=2022)
book.save()
# NEW WAY: Update multiple fields in-place
book.update(title="New title", year=2022)
}}}
This way we can save on typing, and set fields (especially if we have them
in a dict) easily. Or we can update an object with new field values and
save it explicitly.
It can be implemented easily in the base Model class by adding two
methods:
{{{
class Model:
def set(self, **kwargs):
for name, value ink wargs.items():
setattr(name, value)
def update(self, **kwargs):
self.set(kwargs)
return self.save()
}}}
My question is next - why it's no such obvious operations in the Django
ORM yet? Is it done by purpose?
--
Ticket URL: <https://code.djangoproject.com/ticket/33834>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070181dd0660ce-0c171876-ab9b-4fdf-a5be-e6c9a5d58c7e-000000%40eu-central-1.amazonses.com.