Explain Meta.connection
janus opened this issue · 2 comments
Could you example the below?
return cls.Meta.connection.cursor()
I don't really understand where Meta.connection gets it methods.
In the example from the README
, we see this:
class Meta:
connection = sqlite3.connect(":memory:")
defined within the Model
subclass. What this does is define another class as a class attribute of the Model
subclass. This other class, Meta
, that can be accessed from cls.Meta
within classmethods. The Meta
class then has a class attribute, connection
. When the user (user being the programmer using this ORM) defines a new model, they have to provide an active database connection to their Model
subclass. That is what the above code is doing. The sqlite3.connect
method returns a connection to an in-memory sqlite database. The Model
subclass then uses that connection to persist data.
The cursor()
method on the Meta.connection
object returns a database cursor for the database connection. You can find more information about the python sqlite
module here: http://docs.python.org/library/sqlite3.html
Sorry, yes, @pwoolcoc's explanation is exactly correct. It's just how configuration for the ORM is stored, it's not entirely elegant as some class attributes get lost when redefining the meta class, but I probably need to add some code to put them back on initialization (for example, it shouldn't fail if indexes
is missing).