Saving a model sets the sort field index to 0

Hey there,
I have an issue with the creation of the model (or note type) for my addon.
I have following method:

def add_x_model(col: Collection) -> NoteType:
    """
    Adds the smr model to the collection and returns it
    :param col: anki's collection
    :return: the smr model
    """
    x_model: NoteType = col.models.new(X_MODEL_NAME)
    # Add fields:
    for field_identifier in SMR_FIELD_IDENTIFIERS:
        fld: Dict = col.models.newField(SMR_NOTE_FIELD_NAMES[field_identifier])
        col.models.addField(x_model, fld)
    # Add templates
    for cid, name in enumerate(X_CARD_NAMES, start=1):
        template: Dict = col.models.newTemplate(name)
        card: List[str] = generate_card_template(cid)
        template['qfmt'] = card[0]
        template['afmt'] = card[1]
        col.models.addTemplate(x_model, template)
    col.models.set_sort_index(nt=x_model, idx=SMR_FIELD_IDENTIFIERS.index(X_SORT_FIELD))
    set_x_model_fields(x_model)
    col.models.add(x_model)
    return x_model

Modifying the sort field with col.models.set_sort_index() does work and sets the sort field index to 22 as wished. However, after calling col.models.add(), the sort index is set to 0 again. What can I do to avoid the sort index from being set to 0 when I save the model?

I’ll add this to the todo list, but for now I imagine you can work around it by adding the note type, then adjusting the sort index and saving it.

Editing the sortfield after adding the model and saving it again worked. Thank you very much for the fast response :+1: