peewee - armeggiare intorno con un ORM.
definizioni di modello e creazione dello schema
puzza django:
import Peewee
Classe Blog (peewee.Model):
& Nbsp; title = peewee.CharField ()
& Nbsp; def __unicode __ (self):
& Nbsp; ritorno self.title
Classe 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 __ (self):
& Nbsp; return '% s:% s'% (self.blog.title, self.title)
creare alcune tabelle:
>>> Blog.create_table ()
>>> Entry.create_table ()
chiavi esterne funzionano come Django di
& Nbsp; >>> b = Blog (title = "del Peewee Big Adventure")
& Nbsp; >>> b.save ()
& Nbsp; >>> e = Entry (title = "movie Greatest mai", content = "! YES", blog = b)
& Nbsp; >>> e.save ()
& Nbsp; >>> e.blog
& Nbsp;
& Nbsp; >>> per e in b.entry_set:
& Nbsp; ... print e.title
& Nbsp; ...
& Nbsp; più grande film mai?
bizzarro interrogazione
query sono disponibili in 4 gusti (selezionare / update / insert / delete):
>>> For i in xrange (50):
... B = Blog (title = 'blog-% d'% i)
... B.save ()
... Per j in xrange (i):
... E = Entry (title = 'entry% d'% j, blog = b)
... E.save ()
...
>>> [Obj.title per obj in Blog.select (). (Dove title__contains = '0')]
[U'blog-0 ', u'blog-10', u'blog-20 ', u'blog-30', u'blog-40 ']
>>> [Obj.title per obj in Blog.select (). Compagine (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 per obj in Blog.select (). Join (Entry) .dove (title__contains = 'entry-45')]
[U'blog-46 ', u'blog-47', u'blog-48 ', u'blog-49']
>>> Blog.select (). Join (Entry) .dove («entry-29 'title__contains =). Count ()
20
Requisiti :
- Python
I commenti non trovato