I suggest that all dzhangistam / alchemists respond a little and read a free interpretation of the introductory tutorial and part of the documentation on Peewee - stand-alone ORM, which is mandatory for any python artist and, in particular, a flysker. They write about her a little, but in vain. It is very easy to make friends with Peewee, especially if you are already familiar with any ORM on ActiveRecord. More importantly, it's nice to be friends with her :) Well, let's begin.
pip install peewee
git clone https://github.com/coleifer/peewee.git cd peewee python setup.py install
python setup.py test
pip install flask-peewee
from peewee import * db = SqliteDatabase('people.db') class Person(Model): name = CharField() birthday = DateField() is_relative = BooleanField() class Meta: database = db # 'people.db'
null=False
- is it possible to store null values;index=False
- whether to create an index for a given column in the database;unique=False
- whether to create a unique index for a given column in the database. See also the chapter on composite indices ;verbose_name=None
- the string for the understandable representation of the field;help_text=None
- a string with auxiliary text for the field;db_column=None
- a string that explicitly defines the name of a column in the database for this field, for example, when working with a legacy database;default=None
- the default=None
value for class fields during instantiation;choices=None
- a list or a tuple of two-element tuples, where the first element is the value for the base, the second is the displayed value (similar to jang);primary_key=False
- whether to use this field as the primary key;sequence=None
- the sequence for filling the field (make sure that the backend supports this functionality);class Meta
:Option | Description | Inherited? |
---|---|---|
database | model database | Yes |
db_table | the name of the table in which the data will be stored | not |
indexes | list of fields to index | Yes |
order_by | the list of fields to sort by default | Yes |
primary_key | composite primary key, instance of the CompositeKey class, example | Yes |
table_alias | alias tables for use in queries | not |
class Pet(Model): owner = ForeignKeyField(Person, related_name='pets') name = CharField() animal_type = CharField() class Meta: database = db # 'people.db'
>>> Person.create_table() >>> Pet.create_table()
>>> from datetime import date >>> uncle_bob = Person(name='Bob', birthday=date(1960, 1, 15), is_relative=True) >>> uncle_bob.save() # c
>>> grandma = Person.create(name='Grandma', birthday=date(1935, 3, 1), is_relative=True) >>> herb = Person.create(name='Herb', birthday=date(1950, 5, 5), is_relative=False)
>>> grandma.name = 'Grandma L.' >>> grandma.save() # grandma
>>> 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')
>>> herb_mittens.delete_instance() # , 1
>>> herb_fido.owner = uncle_bob >>> herb_fido.save() >>> bob_fido = herb_fido #
SelectQuery.get()
method: >>> grandma = Person.select().where(Person.name == 'Grandma L.').get()
get()
: >>> grandma = Person.get(Person.name == 'Grandma L.')
Person
loop: >>> for person in Person.select(): ... print person.name, person.is_relative ... Bob True Grandma L. True Herb False
Person
instances and all the related records: >>> for person in Person.select(): ... print person.name, person.pets.count(), 'pets' ... for pet in person.pets: ... print ' ', pet.name, pet.animal_type ... Bob 2 pets Kitty cat Fido dog Grandma L. 0 pets Herb 1 pets Mittens Jr cat
>>> for pet in Pet.select().where(Pet.animal_type == 'cat'): ... print pet.name, pet.owner.name ... Kitty Bob Mittens Jr Herb
# >>> for pet in Pet.select().join(Person).where(Person.name == 'Bob'): ... print pet.name ... Kitty Fido
>>> for pet in Pet.select().where(Pet.owner == uncle_bob): ... print pet.name
SelectQuery.order_by()
method: >>> for pet in Pet.select().where(Pet.owner == uncle_bob).order_by(Pet.name): ... print pet.name ... Fido Kitty
>>> for person in Person.select().order_by(Person.birthday.desc()): ... print person.name ... Bob Herb Grandma L.
>>> d1940 = date(1940, 1, 1) >>> d1960 = date(1960, 1, 1) >>> for person in Person.select().where((Person.birthday < d1940) | (Person.birthday > d1960)): ... print person.name ... Bob Grandma L.
where((Person.birthday < d1940) | (Person.birthday > d1960))
can also be written as where(Person.birthday < d1940 or Person.birthday > d1960)
, but it is better not to do this, since A peewee does not always correctly handle such an entry. >>> for person in Person.select().where((Person.birthday > d1940) & (Person.birthday < d1960)): ... print person.name ... Herb
>>> for person in Person.select().where(fn.Lower(fn.Substr(Person.name, 1, 1)) == 'g'): ... print person.name ... Grandma L.
SelectQuery.group_by()
SelectQuery.having()
SelectQuery.limit()
and SelectQuery.offset()
It is a quicker way to find out how to use it.
On my computer, peewee walked around Django and SQLAlchemy on most tasks, and showed comparable results in iterations and sample instances.
Source: https://habr.com/ru/post/207110/
All Articles