Code to create cards

Hello,

I am trying to write my own add on in Python. Sorry to ask a very dumb question, but I have not been able to find the code to add a card to the database.

I had a look at this resource but that did not answer my questions: https://addon-docs.ankiweb.net/#/getting-started?id=the-collection

Would you know which code enables to select a card template, populate its fields and assign it to a deck? That would be very helpful.

Many thanks !

1 Like

Found the solution. In case that may help somebody else, here is the code

model = mw.col.models.byName("Basic")
# mw.col.models.setCurrent(model)
new_note = mw.col.newNote(model)

did = mw.col.decks.id("Temporary")
new_note.model()["did"] = did

new_note["Recto"] = "test recto"
new_note["Verso"] = "test verso"

mw.col.addNote(new_note)

mw.col.reset()
mw.reset()
2 Likes