Skip to content

Recommended pattern to update a "huge" model (ormar + fastapi) #414

Answered by collerek
igormorgado asked this question in Q&A
Discussion options

You must be logged in to vote

If you refresh or fetch the model before updating you already have all the values as of pre-update state.
That means that you can simply pass all the values you have in your user if they are set and only those values will be updated.
Of course, that poses a problem if you really want to nullify the field, but that would be the same in your if/else scenario.

# in your update function you can simply pass the dict as kwargs
user_object = User.objects.filter(username=username).get()
return user_object.update(**{k: v for k, v in user.dict() if v})

If you want, you can further exclude the fields to limit it further (i.e. do not allow password updates regardless if the field is there) you can ut…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by igormorgado
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants