kerrickstaley / genanki

A Python 3 library for generating Anki decks
MIT License
1.99k stars 150 forks source link

Is there a way to UPDATE a Model object's attributes? (fields & templates) #56

Closed gelodefaultbrain closed 3 years ago

gelodefaultbrain commented 3 years ago

hello! Thank you so much for this library! It has been really helpful!

I just got a question cause once you create an object of type Model you're going to pass in fields and template right... So in my program I'll be needing to somehow UPDATE those fields and templates depending on the Question and Answer data that I have so my question is... Is there a function or setter method that would update the fields and templates attribute in the class?

I know I can make a new instance of a model over and over again but I think that's not programmatically efficient so I am asking the devs about this if they have included it... I know I can also call dir() to see the methods and review the code but I would like to ask it here and probably learn more about it when I talk to one of the developers.

Example here: So when I create that my_model is there a way to update the attributes? Can I just simply do a .fields = #something same with templates? I was wondering if there could be a function for it.,

my_model = genanki.Model(1607392319,'Simple Model',
  fields=[
    {'name': 'Question'},
    {'name': 'Answer'}
  ],
  templates=[
    {
      'name': 'Card 1',
      'qfmt': '{{Question}}',
      'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
    },

  ])

Again THANK YOU FOR THIS! SUCH A HELPFUL LIBRARY!

gelodefaultbrain commented 3 years ago

Got it! these are the functions I am referring to. Good thing you have this guys! GREAT LIBRARY THANKS!

  def set_fields(self, fields):
    if isinstance(fields, list):
      self.fields = fields
    elif isinstance(fields, str):
      self.fields = yaml.full_load(fields)

  def set_templates(self, templates):
    if isinstance(templates, list):
      self.templates = templates
    elif isinstance(templates, str):
      self.templates = yaml.full_load(templates)