Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cast keyword argument to QueryBuilder.update() #832

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1385,14 +1385,15 @@ def skip(self, *args, **kwargs):
"""Alias for limit method"""
return self.offset(*args, **kwargs)

def update(self, updates: dict, dry=False, force=False):
def update(self, updates: dict, dry=False, force=False, cast=False):
"""Specifies columns and values to be updated.

Arguments:
updates {dictionary} -- A dictionary of columns and values to update.

Keyword Arguments:
dry {bool} -- Whether the query should be executed. (default: {False})
cast {bool} -- Apply model casts to provided dictionary. (default: {False})

Returns:
self
Expand All @@ -1410,6 +1411,14 @@ def update(self, updates: dict, dry=False, force=False):

self.observe_events(model, "updating")

# cast model values if needed
if model and updates and cast:
for key, value in updates.items():
if model.__casts__.get(key):
caster_name = model.__casts__[key]
caster = model.__cast_map__[caster_name]
updates[key] = caster().set(value)

# update only attributes with changes
if model and not model.__force_update__ and not force:
changes = {}
Expand Down