anki.models
===========

.. py:module:: anki.models


Attributes
----------

.. autoapisummary::

   anki.models.NotetypeNameId
   anki.models.NotetypeNameIdUseCount
   anki.models.NotetypeNames
   anki.models.ChangeNotetypeInfo
   anki.models.ChangeNotetypeRequest
   anki.models.StockNotetype
   anki.models.NotetypeDict
   anki.models.NoteType
   anki.models.FieldDict
   anki.models.TemplateDict
   anki.models.NotetypeId


Classes
-------

.. autoapisummary::

   anki.models.ModelsDictProxy
   anki.models.ModelManager


Module Contents
---------------

.. py:data:: NotetypeNameId

.. py:data:: NotetypeNameIdUseCount

.. py:data:: NotetypeNames

.. py:data:: ChangeNotetypeInfo

.. py:data:: ChangeNotetypeRequest

.. py:data:: StockNotetype

.. py:data:: NotetypeDict

.. py:data:: NoteType

.. py:data:: FieldDict

.. py:data:: TemplateDict

.. py:data:: NotetypeId

.. py:class:: ModelsDictProxy(col: anki.collection.Collection)

   .. py:method:: keys() -> Any


   .. py:method:: values() -> Any


   .. py:method:: items() -> Any


.. py:class:: ModelManager(col: anki.collection.Collection)

   Bases: :py:obj:`anki._legacy.DeprecatedNamesMixin`


   .. py:attribute:: col


   .. py:attribute:: models


   .. py:method:: all_names_and_ids() -> collections.abc.Sequence[NotetypeNameId]


   .. py:method:: all_use_counts() -> collections.abc.Sequence[NotetypeNameIdUseCount]


   .. py:method:: have(id: NotetypeId) -> bool


   .. py:method:: current(for_deck: bool = True) -> NotetypeDict

      Get current model. In new code, prefer col.defaults_for_adding()



   .. py:method:: id_for_name(name: str) -> NotetypeId | None


   .. py:method:: get(id: NotetypeId) -> NotetypeDict | None

      Get model with ID, or None.

      This returns a reference to a cached dict. Copy the returned model before modifying it if you're not calling .update_dict() afterward.



   .. py:method:: all() -> list[NotetypeDict]

      Get all models.



   .. py:method:: by_name(name: str) -> NotetypeDict | None

      Get model with NAME.



   .. py:method:: new(name: str) -> NotetypeDict

      Create a new model, and return it.



   .. py:method:: remove_all_notetypes() -> None


   .. py:method:: remove(id: NotetypeId) -> anki.collection.OpChanges

      Modifies schema.



   .. py:method:: add(notetype: NotetypeDict) -> anki.collection.OpChangesWithId

      Replaced with add_dict()



   .. py:method:: add_dict(notetype: NotetypeDict) -> anki.collection.OpChangesWithId

      Notetype needs to be fetched from DB after adding.



   .. py:method:: ensure_name_unique(notetype: NotetypeDict) -> None


   .. py:method:: update_dict(notetype: NotetypeDict, skip_checks: bool = False) -> anki.collection.OpChanges

      Update a NotetypeDict. Caller will need to re-load notetype if new fields/cards added.



   .. py:method:: nids(ntid: NotetypeId) -> list[anki.notes.NoteId]

      Note ids for M.



   .. py:method:: use_count(notetype: NotetypeDict) -> int

      Number of note using M.



   .. py:method:: copy(notetype: NotetypeDict, add: bool = True) -> NotetypeDict

      Copy, save and return.



   .. py:method:: field_map(notetype: NotetypeDict) -> dict[str, tuple[int, FieldDict]]

      Mapping of field name -> (ord, field).



   .. py:method:: field_names(notetype: NotetypeDict) -> list[str]


   .. py:method:: sort_idx(notetype: NotetypeDict) -> int


   .. py:method:: cloze_fields(mid: NotetypeId) -> collections.abc.Sequence[int]

      The list of index of fields that are used by cloze deletion in the note type with id `mid`.



   .. py:method:: new_field(name: str) -> FieldDict


   .. py:method:: add_field(notetype: NotetypeDict, field: FieldDict) -> None

      Modifies schema.



   .. py:method:: remove_field(notetype: NotetypeDict, field: FieldDict) -> None

      Modifies schema.



   .. py:method:: reposition_field(notetype: NotetypeDict, field: FieldDict, idx: int) -> None

      Modifies schema.



   .. py:method:: rename_field(notetype: NotetypeDict, field: FieldDict, new_name: str) -> None


   .. py:method:: set_sort_index(notetype: NotetypeDict, idx: int) -> None

      Modifies schema.



   .. py:method:: new_template(name: str) -> TemplateDict


   .. py:method:: add_template(notetype: NotetypeDict, template: TemplateDict) -> None

      Modifies schema.



   .. py:method:: remove_template(notetype: NotetypeDict, template: TemplateDict) -> None

      Modifies schema.



   .. py:method:: reposition_template(notetype: NotetypeDict, template: TemplateDict, idx: int) -> None

      Modifies schema.



   .. py:method:: template_use_count(ntid: NotetypeId, ord: int) -> int


   .. py:method:: get_single_notetype_of_notes(note_ids: collections.abc.Sequence[anki.notes.NoteId]) -> NotetypeId


   .. py:method:: change_notetype_info(*, old_notetype_id: NotetypeId, new_notetype_id: NotetypeId) -> ChangeNotetypeInfo


   .. py:method:: change_notetype_of_notes(input: ChangeNotetypeRequest) -> anki.collection.OpChanges

      Assign a new notetype, optionally altering field/template order.

      To get defaults, use

      info = col.models.change_notetype_info(...)
      input = info.input
      input.note_ids.extend([...])

      The new_fields and new_templates lists are relative to the new notetype's
      field/template count. Each value represents the index in the previous
      notetype. -1 indicates the original value will be discarded.



   .. py:method:: restore_notetype_to_stock(notetype_id: NotetypeId, force_kind: anki.stdmodels.StockNotetypeKind.V | None) -> anki.collection.OpChanges


   .. py:method:: change(notetype: NotetypeDict, nids: list[anki.notes.NoteId], newModel: NotetypeDict, fmap: dict[int, int | None], cmap: dict[int, int | None] | None) -> None


   .. py:method:: scmhash(notetype: NotetypeDict) -> str

      Return a hash of the schema, to see if models are compatible.



   .. py:method:: addTemplate(notetype: NotetypeDict, template: TemplateDict) -> None


   .. py:method:: remTemplate(notetype: NotetypeDict, template: TemplateDict) -> None


   .. py:method:: move_template(notetype: NotetypeDict, template: TemplateDict, idx: int) -> None


   .. py:method:: addField(notetype: NotetypeDict, field: FieldDict) -> None


   .. py:method:: remField(notetype: NotetypeDict, field: FieldDict) -> None


   .. py:method:: moveField(notetype: NotetypeDict, field: FieldDict, idx: int) -> None


   .. py:method:: renameField(notetype: NotetypeDict, field: FieldDict, new_name: str) -> None


   .. py:method:: rem(m: NotetypeDict) -> None

      Delete model, and all its cards/notes.



   .. py:method:: set_current(m: NotetypeDict) -> None


   .. py:method:: all_names() -> list[str]


   .. py:method:: ids() -> list[NotetypeId]


   .. py:method:: flush() -> None


   .. py:method:: update(notetype: NotetypeDict, preserve_usn: bool = True, skip_checks: bool = False) -> None

      Add or update an existing model. Use .update_dict() instead.



   .. py:method:: save(notetype: NotetypeDict | None = None, **legacy_kwargs: bool) -> None

      Save changes made to provided note type.



