#Sora #peewee plus celery =?

Source: Internet
Author: User

Lately crazy looking full time Hunter


Preliminary study of PeeWee, because SQLAlchemy is really too heavyweight, document is too disgusting, PeeWee easy to get started much easier, also very light

Combining celery and PeeWee, wrote something, inquiring about age

myapp/db.py:

from __future__ import absolute_importfrom myapp.celery import appfrom  Peewee import *db = sqlitedatabase (' people.db ') Class person (Model):     name = charfield ()     age = integerfield ()      class meta:        database = db@app.taskdef  agequery (name):     db.connect ()  #   collection = []     for i in person.select (). where (Person.name == name):        return {' name ': Name, ' Age ':i.age}  #     Collection.append ({' name ': Name, ' Age ': i.age})   #  return collection     db.close () @app. Taskdef savedata (message):     db.connect ()      newdata = person.cReate (Name=message.get (' name '), Age=message.get (' age '))     newdata.save ()      db.close () @app. Taskdef initdb ():     db.connect ()      Db.create_tables ([person,])     db.close ()

Test Case:

python 2.7.6  (default, mar 22 2014, 22:59:56)  [gcc 4.8.2] on  linux2Type  "Help",  "copyright",  "credits"  or  "license"  for more  information.>>> from myapp.db import savedata,agequery>>> res  = agequery (' miaoting ') >>> res = agequery.delay (' miaoting ') >>>  Res.get () {u ' age ':  17, u ' name ':  u ' miaoting '}>>> savedata ({' name ': ' Jc ', ' Age ': 65}) >>> savedata.delay ({' name ': ' Jc ', ' age ': +}) <AsyncResult:  C6d06e50-42b3-42a1-b06b-2561eb0c64af>>>> res = agequery.delay (' Jc ') >>>  res.get () {u ' age ':  65, u ' name ':  u ' Jc '}>>> savedata.delay ({' name ': ' Fanshu ', ' Age ': ') <AsyncResult: a348b074-a6f1-430c-af78-42d8e29cf4e6>>>> res =  Agequery.delay (' Fanshu ') >>> res.get () >>>&Nbsp;res = agequery.delay (' JC ') >>> res.get () {u ' age ':  65, u ' name ':  u ' JC ' }>>> res = agequery.delay (' Fanshu ') >>> res.get () >>> res  = agequery.delay (' Fanshu ') >>> res.get ()


The first time you use DB is initialized with Initdb, unless you delete the SQLite database, you only need to call SaveData and Agequery



PeeWee Notes:

PeeWee Study:

In PeeWee, a class that inherits the model class is defined as a table in a database

Feild in a class, as a column (field) of a database

A model class instance is a row


Example

From PeeWee Import *


db = Sqlitedatabase (' people.db ')


Class Person (Model):

Name = Charfield ()

Birthday = Datefield ()

Is_relative = Booleanfield ()

Class Meta:

Database = db # This model uses the "people.db" database.



Class Pet (Model):

Owner = Foreignkeyfield (person, related_name= ' pets ')

Name = Charfield ()

Animal_type = Charfield ()

Class Meta:

Database = db # This model uses the "PEOPLE.DB" database


Db.connect ()


Db.create_tables ([Person, Pet])



Save data:

>>> from datetime import Date

>>> Uncle_bob = person (name= ' Bob ', Birthday=date (1960, 1, +), is_relative=true)

>>> Uncle_bob.save () # Bob is now stored in the database

1


>>> Bob_kitty = pet.create (owner=uncle_bob, name= ' Kitty ', animal_type= ' cat ')

>>> Herb_fido = pet.create (Owner=herb, name= ' Fido ', animal_type= ' dog ')

>>> Herb_mittens = pet.create (Owner=herb, name= ' mittens ', animal_type= ' cat ')

>>> Herb_mittens_jr = pet.create (Owner=herb, Name= ' Mittens Jr ', animal_type= ' cat ')


Delete data:

>>> herb_mittens.delete_instance () # He had a great life

1



Querying data (accessing data for related fields based on the feild of the Class):

>>> grandma = Person.select (). WHERE (Person.name = = ' Grandma L. '). Get ()


>>> grandma = person.get (Person.name = = ' Grandma L. ')




List all records:

>>> for WHO in Person.select ():

... print Person.name, person.is_relative

...

Bob True

Grandma L. True

Herb False



Compare the differences between the two types of queries:

>>> query = Pet.select (). WHERE (Pet.animal_type = = ' Cat ')

>>> for pet in query:

... print Pet.name, Pet.owner.name

...

Kitty Bob

Mittens Jr Herb


>>> query = (Pet

...          . Select (Pet, person)

...          . Join (person)

...          . WHERE (Pet.animal_type = = ' Cat '))

>>> for pet in query:

... print Pet.name, Pet.owner.name

...

Kitty Bob

Mittens Jr Herb


The latter is more efficient and the latter does not make unnecessary inquiries


#Sora #peewee plus celery =?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.