Python’s SQLAlchemy vs Other ORMs[转发 3]Django's ORM

Django's ORM

Django is a free and open source web application framework whose ORM is built tightly into the system. After its initial release, Django becomes more and more popular due to its straightforward design and easy-to-use web-ready features. It was released under the BSD license in July 2005. Since Django's ORM is built tightly into the web framework, it's not recommended, although possible, to use its ORM in a standalone non-Django Python application.

Django, one of the most popular Python web frameworks, has its own dedicated ORM. Compared to SQLAlchemy, Django's ORM is more geared towards direct SQL object manipulation where it exposes a plain and direct mapping between database tables and Python classes.

 1 $ django-admin.py startproject demo
 2 $ cd demo
 3 $ python manage.py syncdb
 4 Creating tables ...
 5 Creating table django_admin_log
 6 Creating table auth_permission
 7 Creating table auth_group_permissions
 8 Creating table auth_group
 9 Creating table auth_user_groups
10 Creating table auth_user_user_permissions
11 Creating table auth_user
12 Creating table django_content_type
13 Creating table django_session
14  
15 You just installed Django's auth system, which means you don't have any superusers defined.
16 Would you like to create one now? (yes/no): no
17 Installing custom SQL ...
18 Installing indexes ...
19 Installed 0 object(s) from 0 fixture(s)
20 $ python manage.py shell

Since we cannot execute Django's code without creating a project first, we created a Django project 'demo' in the previous shell and entered the Django shell to test our ORM example.

 1 # demo/models.py
 2 >>> from django.db import models
 3 >>>
 4 >>>
 5 >>> class Person(models.Model):
 6 ...     name = models.TextField()
 7 ...
 8 ...     class Meta:
 9 ...         app_label = 'demo'
10 ...
11 >>>
12 >>> class Address(models.Model):
13 ...     address = models.TextField()
14 ...     person = models.ForeignKey(Person)
15 ...
16 ...     class Meta:
17 ...         app_label = 'demo'

The code above declared two Python classes, Person and Address, each of which maps to a database table. Before execute any database manipulation code, we need to create the tables in a local sqlite database.

1 python manage.py syncdb
2 Creating tables ...
3 Creating table demo_person
4 Creating table demo_address
5 Installing custom SQL ...
6 Installing indexes ...
7 Installed 0 object(s) from 0 fixture(s)

To insert a person and an address into the database, we instantiate the corresponding objects and call the save()methods of those objects.

1 >>> from demo.models import Person, Address
2 >>> p = Person(name='person')
3 >>> p.save()
4 >>> print "%r, %r" % (p.id, p.name)
5 1, 'person'
6 >>> a = Address(person=p, address='address')
7 >>> a.save()
8 >>> print "%r, %r" % (a.id, a.address)
9 1, 'address'

To get or retrieve the person and address objects, we use the magical objects attribute of the model classes to fetch the objects from the database.

 1 >>> persons = Person.objects.filter(name='person')
 2 >>> persons
 3 []
 4 >>> p = persons[0]
 5 >>> print "%r, %r" % (p.id, p.name)
 6 1, u'person'
 7 >>> addresses = Address.objects.filter(person=p)
 8 >>> addresses
 9 [<address>]
10 >>> a = addresses[0]
11 >>> print "%r, %r" % (a.id, a.address)
12 1, u'address'

 

posted @ 2016-11-30 10:06  Brian Tan  阅读(244)  评论(0编辑  收藏  举报