peewee - gehannes rond met een ORM.
model definities en schema creatie
ruikt naar django:
import peewee
klasse Blog (peewee.Model):
& Nbsp; title = peewee.CharField ()
& Nbsp; def __unicode __ (zelf):
& Nbsp; return self.title
klasse Entry (peewee.Model):
& Nbsp; title = peewee.CharField (max_length = 50)
& Nbsp; content = peewee.TextField ()
& Nbsp; pub_date = peewee.DateTimeField ()
& Nbsp; blog = peewee.ForeignKeyField (Blog)
& Nbsp; def __unicode __ (zelf):
& Nbsp; return '% s:% s'% (self.blog.title, self.title)
Maak een aantal tabellen:
>>> Blog.create_table ()
>>> Entry.create_table ()
buitenlandse sleutels werken als Django's
& Nbsp; >>> b = Blog (title = "Peewee's Big Adventure ')
& Nbsp; >>> b.save ()
& Nbsp; >>> e = Entry (title = "? Greatest film ooit", content = "YES!", Blog = b)
& Nbsp; >>> e.save ()
& Nbsp; >>> e.blog
& Nbsp;
& Nbsp; >>> voor e in b.entry_set:
& Nbsp; ... print e.title
& Nbsp; ...
& Nbsp; Greatest film ooit?
bizarre opvragen
query's zijn er in 4 smaken (selecteren / bijwerken / insert / delete):
>>> For i in xrange (50):
... B = Blog (title = 'blog-% d'% i)
... B.save ()
... Voor j in xrange (i):
... E = Entry (title = 'entry-% d'% j, blog = b)
... E.save ()
...
>>> [Obj.title voor obj in Blog.select (). Waarin (title__contains = '0')]
[U'blog-0 ', u'blog-10', u'blog-20 ', u'blog-30', u'blog-40 ']
>>> [Obj.title voor obj in Blog.select (). Pagineert (3, 10)]
[U'blog-20 ', u'blog-21', u'blog-22 ', u'blog-23', u'blog-24 ',
& Nbsp; u'blog-25 ', u'blog-26', u'blog-27 ', u'blog-28', u'blog-29 ']
>>> [Obj.title voor obj in Blog.select (). Join (Entry) .Waar (title__contains = "entry-45 ')]
[U'blog-46 ', u'blog-47', u'blog-48 ', u'blog-49']
>>> Blog.select (). Join (Entry) .Waar (title__contains = "entry-29 '). Optellen ()
20
Eisen
- Python
Reacties niet gevonden