Mapping Python objects to database rows manually
We can map SQL rows to class definitions so that we can create proper Python object instances from the data in a database. If we're careful with our database and class definitions, this isn't impossibly complex. If, however, we're careless, we can create Python objects where the SQL representation is quite complex. One consequence of the complexity is that numerous queries are involved in mapping between object and database rows. The challenge is to strike a balance between object-oriented design and the constraints imposed by the SQL database.
We will have to modify our class definitions to be more aware of the SQL implementation. We'll make several modifications to the Blog
and Post
class designs shown in Chapter 10, Storing and Retrieving Objects via Shelve.
Here's a Blog
class definition:
from collections import defaultdict class Blog: def __init__( self, **kw ): """Requires title""" self.id= kw.pop('id', None) ...