DomainHOWTO

De Stoq Wiki
Ir para: navegação, pesquisa

Creating a new domain class

To define a new domain class you just need to subclass the Domain class found in stoqlib.domain.base:

from stoqlib.domain.base import Domain

class Building(Domain):
  pass

That's it. However, while it is a valid class it is not very interesting. To be able to store something in the object we need to define a column.

from sqlobject.col import UnicodeCol

class Building(Domain):
  name = UnicodeCol()

Creating a new object

from stoqlib.database.runtime import new_transaction

trans = new_transaction()
Building(name="My building", connection=trans)

Querying objects

for building in Building.select(connection=conn):
   print building

It's easy to extent the select by adding conditions:

buildings = Building.select(Building.q.name == "My building")

which is equal to

buildings = Building.selectBy(name="My building")

Both of them will return all the buildings which has the name attribute set to "My building"