Sobes.tech
Junior — Middle

How is the complete list of all models in the Ormar library obtained?

sobes.tech AI

Answer from AI

In the Ormar library, to get a complete list of all models, you can use the attribute ormar.ModelMeta.models or store models in a separate list during their definition.

Example of how to gather all models:

import ormar

class BaseMeta(ormar.ModelMeta):
    database = ...
    metadata = ...

class Model1(ormar.Model):
    class Meta(BaseMeta):
        tablename = "model1"
    id = ormar.Integer(primary_key=True)

class Model2(ormar.Model):
    class Meta(BaseMeta):
        tablename = "model2"
    id = ormar.Integer(primary_key=True)

# List of models
all_models = [Model1, Model2]

# Or, if needed dynamically, you can use module introspection

Ormar does not provide a built-in method for automatically retrieving all models, so they are usually collected manually or through reflection on the module where they are defined.