Peewee - Update an entry with a dictionary
<p>I found <a href="https://stackoverflow.com/questions/22750439/peewee-how-to-convert-a-dict-into-a-model">this handy answer</a> on how to <strong>create</strong> a new table entry using a dictionary.</p>

<p>Now i'd like to <strong>update</strong> an entry with the same method. but i don't know how to adress the specific table entry that i'd like to update. </p>

<p>My current version looks like this:</p>

<pre><code>entries = Fruit.select().order_by(Fruit.name.desc())
#... all entries are listed with the index number
entry_index = int(input("Please enter entry number: "))
#...
entry_index -= 1

name = "Banana"
color = "yellow"

if input('Update entry? [Yn] ').lower() != 'n':
        entries[entry_index].name = name
        entries[entry_index].color = color
</code></pre>

<p>As you can see i adress every field explicitly.
I would like to put the variables (name, color) in a dictionary and update the entry at position "entry_index" with the fore-mentioned double-star-shortcut <a href="https://stackoverflow.com/questions/22750439/peewee-how-to-convert-a-dict-into-a-model">like in this answer</a>.
But i can't find a <a href="http://docs.peewee-orm.com/en/latest/peewee/querying.html#updating-existing-records" rel="nofollow">proper method in the docs</a>. </p>

<p>Does anyone know how to accomplish that?</p>

<p>Thanks for your help!</p>

<p>Muff</p>

<p>The double star shortcut is a python thing. It allows you to expand a dictionary into key-word arguments in a method. That means you can do something like this:</p>

<pre><code>fruit = { "name": "Banana", "color": "yellow"}
some_method(**fruit)
</code></pre>

<p>which would be the equivelent of doing:</p>

<pre><code>some_method(name="Banana", color="yellow")
</code></pre>

<p>I don't think that's helpful to you here though. You've already updated the values on the selected entry and all you need to do next is save them.</p>

<p><a href="http://docs.peewee-orm.com/en/latest/peewee/querying.html#updating-existing-records" rel="nofollow">According to the peewee docs</a>, once you have created an instance, any calls to save will cause that instance to be updated. That means you only need to call the <code>save</code> method to update the values you've changed. If you add a final <code>entries[entry_index].save()</code>, that should persist those changes.</p>

python dictionary peewee