SlideShare a Scribd company logo
Python ORMs
Cosmin Poieana
<cmin@ropython.org>
→ baze de date
→ tipuri de ORM (modele)
→ comparații (exemple)
→ peewee
→ resurse
Database APIs
NoSQL
● redis
● mongodb
● …
SQL
● pyodbc
● sqlite3
● pymysql
● ...
sqlite3
● Nu are nevoie de driver, server
● Static pe disc / memorie
● Interogări simple (+ mici diferențe)
1. conexiune
2. obținere cursor
3. execuție interogare
*. commit
4. închidere
sqlite3
>>> import sqlite3
>>> conn = sqlite3.connect("ropy.db")
>>> c = conn.cursor()
>>> c.execute("CREATE TABLE persoana (nume text,
adresa text, varsta real)")
>>> c.execute("INSERT INTO persoana VALUES
('Nume1', 'Adresa1', 20)")
>>> conn.commit()
>>> c.execute("SELECT * FROM persoana")
>>> c.fetchall()
[(u'Nume1', u'Adresa1', 20.0)]
ORM
● Object-relational mapping
(convertire date dintr-un sistem incompatibil in
POO)
● Pony, Django ORM, peewee, SQLAlchemy
● Modele
– DataMapper
– ActiveRecord
● SQLite, MySQL, PostgreSQL[, Oracle]
Django ORM
$ django-admin.py startproject ropy
$ cd ropy
$ python manage.py syncdb
>>> from django.db import models
>>> class Person(models.Model):
... name = models.TextField()
... class Meta:
... app_label = 'demo'
>>> class Address(models.Model):
... address = models.TextField()
... person = models.ForeignKey(Person)
... class Meta:
... app_label = 'demo'
Django ORM
>>> p = Person(name='person')
>>> p.save()
>>> print "%r, %r" % (p.id, p.name)
1, 'person'
>>> a = Address(person=p, address='address')
>>> a.save()
>>> print "%r, %r" % (a.id, a.address)
1, 'address'
Django ORM
>>> persons = Person.objects.filter(name='person')
>>> p = persons[0]
>>> print "%r, %r" % (p.id, p.name)
1, u'person'
>>> addresses = Address.objects.filter(person=p)
>>> addresses
[<address>]
>>> a = addresses[0]
>>> print "%r, %r" % (a.id, a.address)
1, u'address'
Peewee
>>> from peewee import SqliteDatabase, CharField,
ForeignKeyField, Model
>>> db = SqliteDatabase(':memory:')
>>> class Person(Model):
... name = CharField()
... class Meta:
... database = db
>>> class Address(Model):
... address = CharField()
... person = ForeignKeyField(Person)
... class Meta:
... database = db
>>> Person.create_table()
>>> Address.create_table()
Peewee
>>> p = Person(name='person')
>>> p.save()
>>> a = Address(address='address',
person=p)
>>> a.save()
Peewee
>>> person = Person.select().where(Person.name ==
'person').get()
>>> print '%r, %r' % (person.id, person.name)
1, u'person'
>>> address = Address.select().where(Address.person ==
person).get()
>>> print '%r, %r' % (address.id, address.address)
1, u'address'
Resurse
● http://www.pythoncentral.io/sqlalchemy-vs-orms/
● http://peewee.readthedocs.org/en/latest/
● http://effectivedjango.com/orm.html
● http://www.sqlalchemy.org/
● http://ponyorm.com/

More Related Content

PDF
Chrome拡張開発者のためのFirefox拡張開発
PDF
File-I/O -- ist doch ganz einfach, oder?
PDF
言語の設計判断
PPTX
Comets notes
DOCX
RedHat/CentOs Commands for administrative works
PPTX
MongoDb In Action
PDF
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
KEY
Clojure + MongoDB on Heroku
Chrome拡張開発者のためのFirefox拡張開発
File-I/O -- ist doch ganz einfach, oder?
言語の設計判断
Comets notes
RedHat/CentOs Commands for administrative works
MongoDb In Action
Стажировка 2016-07-27 02 Денис Нелюбин. PostgreSQL и jsonb
Clojure + MongoDB on Heroku

What's hot (20)

PDF
GroovyConsole
PDF
MongoDB a document store that won't let you down.
PDF
The Art of Command Line (2021)
PDF
Neoito — *NIX kungfu for web devs
PDF
The Lesser Known Features of ECMAScript 6
KEY
Clojure入門
PDF
Gitkata refspec
PPTX
Mongo db pagination
PDF
Clojure functions
PDF
仮想化環境におけるバイナリー・ポータビリティの考察 (WebAssemblyの場合)
ODP
NUMOSS 4th Week - Commandline Tutorial
PDF
Node lt
PDF
FrozenRails Training
PPTX
node.js workshop- node.js basics
PDF
Mac OS X Lion で作る WordPress local 環境
PDF
Message Decrypt
PDF
Short intro to the Rust language
ODP
Linux Command Line
PPTX
자바스터디 4
PPT
01 linux basics
GroovyConsole
MongoDB a document store that won't let you down.
The Art of Command Line (2021)
Neoito — *NIX kungfu for web devs
The Lesser Known Features of ECMAScript 6
Clojure入門
Gitkata refspec
Mongo db pagination
Clojure functions
仮想化環境におけるバイナリー・ポータビリティの考察 (WebAssemblyの場合)
NUMOSS 4th Week - Commandline Tutorial
Node lt
FrozenRails Training
node.js workshop- node.js basics
Mac OS X Lion で作る WordPress local 環境
Message Decrypt
Short intro to the Rust language
Linux Command Line
자바스터디 4
01 linux basics
Ad

Viewers also liked (20)

PPSX
Almost 2009
PDF
How A Web Page Is Seen
PPT
Krecenje
PPT
21st Century Skills Pp
PPT
Fundraising Day - New York: Taking Leadership Online
PDF
BDM Brochure
PDF
Crew documents 020334 - 020392
PPTX
Dump The Tubes The Economic Case For Led Backlighting Revised
ODP
PPTX
Towards An Integrated Ecosystem Based Management
PPS
Beautiful Lanscape
PDF
Crew, Foia, Documents 011994 - 012108
PPTX
Social Media Basics
PDF
Crew documents 020549 - 020563
PDF
Crew, Foia, Documents 008692 - 008793
PPTX
The Scientific Method on the Semantic Web
PPTX
Another Introduce to Redis
PDF
CAS N° 003 2015
ODP
Digital Native
PDF
Rest In Peace Tribute Tour 7 23 09
Almost 2009
How A Web Page Is Seen
Krecenje
21st Century Skills Pp
Fundraising Day - New York: Taking Leadership Online
BDM Brochure
Crew documents 020334 - 020392
Dump The Tubes The Economic Case For Led Backlighting Revised
Towards An Integrated Ecosystem Based Management
Beautiful Lanscape
Crew, Foia, Documents 011994 - 012108
Social Media Basics
Crew documents 020549 - 020563
Crew, Foia, Documents 008692 - 008793
The Scientific Method on the Semantic Web
Another Introduce to Redis
CAS N° 003 2015
Digital Native
Rest In Peace Tribute Tour 7 23 09
Ad

Similar to Orm (8)

PPTX
25. ORM QuerySet. .pptx
PDF
Python postgre sql a wonderful wedding
PDF
Oleksandr Tarasenko "ORM vs GraphQL"
PDF
ORM vs GraphQL - Python fwdays 2019
PDF
[2019.02.16] hst - orm
ODP
Introduction to SQL Alchemy - SyPy June 2013
PDF
Python BCN Introduction to SQLAlchemy
ODP
Python and MongoDB
25. ORM QuerySet. .pptx
Python postgre sql a wonderful wedding
Oleksandr Tarasenko "ORM vs GraphQL"
ORM vs GraphQL - Python fwdays 2019
[2019.02.16] hst - orm
Introduction to SQL Alchemy - SyPy June 2013
Python BCN Introduction to SQLAlchemy
Python and MongoDB

Orm

  • 1. Python ORMs Cosmin Poieana <cmin@ropython.org> → baze de date → tipuri de ORM (modele) → comparații (exemple) → peewee → resurse
  • 2. Database APIs NoSQL ● redis ● mongodb ● … SQL ● pyodbc ● sqlite3 ● pymysql ● ...
  • 3. sqlite3 ● Nu are nevoie de driver, server ● Static pe disc / memorie ● Interogări simple (+ mici diferențe) 1. conexiune 2. obținere cursor 3. execuție interogare *. commit 4. închidere
  • 4. sqlite3 >>> import sqlite3 >>> conn = sqlite3.connect("ropy.db") >>> c = conn.cursor() >>> c.execute("CREATE TABLE persoana (nume text, adresa text, varsta real)") >>> c.execute("INSERT INTO persoana VALUES ('Nume1', 'Adresa1', 20)") >>> conn.commit() >>> c.execute("SELECT * FROM persoana") >>> c.fetchall() [(u'Nume1', u'Adresa1', 20.0)]
  • 5. ORM ● Object-relational mapping (convertire date dintr-un sistem incompatibil in POO) ● Pony, Django ORM, peewee, SQLAlchemy ● Modele – DataMapper – ActiveRecord ● SQLite, MySQL, PostgreSQL[, Oracle]
  • 6. Django ORM $ django-admin.py startproject ropy $ cd ropy $ python manage.py syncdb >>> from django.db import models >>> class Person(models.Model): ... name = models.TextField() ... class Meta: ... app_label = 'demo' >>> class Address(models.Model): ... address = models.TextField() ... person = models.ForeignKey(Person) ... class Meta: ... app_label = 'demo'
  • 7. Django ORM >>> p = Person(name='person') >>> p.save() >>> print "%r, %r" % (p.id, p.name) 1, 'person' >>> a = Address(person=p, address='address') >>> a.save() >>> print "%r, %r" % (a.id, a.address) 1, 'address'
  • 8. Django ORM >>> persons = Person.objects.filter(name='person') >>> p = persons[0] >>> print "%r, %r" % (p.id, p.name) 1, u'person' >>> addresses = Address.objects.filter(person=p) >>> addresses [<address>] >>> a = addresses[0] >>> print "%r, %r" % (a.id, a.address) 1, u'address'
  • 9. Peewee >>> from peewee import SqliteDatabase, CharField, ForeignKeyField, Model >>> db = SqliteDatabase(':memory:') >>> class Person(Model): ... name = CharField() ... class Meta: ... database = db >>> class Address(Model): ... address = CharField() ... person = ForeignKeyField(Person) ... class Meta: ... database = db >>> Person.create_table() >>> Address.create_table()
  • 10. Peewee >>> p = Person(name='person') >>> p.save() >>> a = Address(address='address', person=p) >>> a.save()
  • 11. Peewee >>> person = Person.select().where(Person.name == 'person').get() >>> print '%r, %r' % (person.id, person.name) 1, u'person' >>> address = Address.select().where(Address.person == person).get() >>> print '%r, %r' % (address.id, address.address) 1, u'address'
  • 12. Resurse ● http://www.pythoncentral.io/sqlalchemy-vs-orms/ ● http://peewee.readthedocs.org/en/latest/ ● http://effectivedjango.com/orm.html ● http://www.sqlalchemy.org/ ● http://ponyorm.com/