advertisement

Django O/R Mapper

26 %
74 %
advertisement
Information about Django O/R Mapper
Technology

Published on January 23, 2009

Author: IanMLewis

Source: slideshare.net

Description

This is a slideshow from a presentation on the Django O/R Mapper I gave for BPStudy (BeProud's monthly event).
advertisement

Django O/R Mapper

I'll be giving this presentation in English. このプレゼンを英語でさせて頂きます。

このプレゼンを英語でさせて頂きます。

Just joking. 嘘です。

嘘です。

自己紹介 名前: イアン・ルイス ( Ian Lewis )‏ 年齢: 27 歳 国籍:米国 Twitter: IanMLewis ( 80 %日本語) HP: http://www.ianlewis.org/ 所属 : BeProud   好きな物: 勉強。挑戦。 Python 。

名前: イアン・ルイス ( Ian Lewis )‏

年齢: 27 歳

国籍:米国

Twitter: IanMLewis ( 80 %日本語)

HP: http://www.ianlewis.org/

所属 : BeProud

 

好きな物: 勉強。挑戦。 Python 。

Ask me questions anytime. いつでも、質問をしても良い

いつでも、質問をしても良い

Why an O/R Mapper? >>> import MySQLdb >>> db=MySQLdb. connect (passwd= &quot;moonpie&quot; ,db= &quot;mydb&quot; ) >>> >>> c=db. cursor () >>> max_price=5 >>> c. execute ( &quot;&quot;&quot;SELECT name, spam, eggs, sausage FROM breakfast ... WHERE price < %s &quot;&quot;&quot; , (max_price,)) >>> c. fetchall () ( ( &quot;Spam and Sausage Lover's Plate&quot; , 5L, 1L, 8L ), ( &quot;Not So Much Spam Plate&quot; , 3L, 2L, 0L ), ( &quot;Don't Wany ANY SPAM! Plate&quot; , 0L, 4L, 3L ) ) >>> c.executemany( ... &quot;&quot;&quot;INSERT INTO breakfast (name, spam, eggs, sausage, price) ... VALUES ( %s , %s , %s , %s , %s )&quot;&quot;&quot; , ... [ ... ( &quot;Spam and Sausage Lover's Plate&quot; , 5, 1, 8, 7.95 ), ... ( &quot;Not So Much Spam Plate&quot; , 3, 2, 0, 3.95 ), ... ( &quot;Don't Wany ANY SPAM! Plate&quot; , 0, 4, 3, 5.95 ) ... ] )‏

>>> import MySQLdb >>> db=MySQLdb. connect (passwd= &quot;moonpie&quot; ,db= &quot;mydb&quot; ) >>> >>> c=db. cursor () >>> max_price=5 >>> c. execute ( &quot;&quot;&quot;SELECT name, spam, eggs, sausage FROM breakfast ... WHERE price < %s &quot;&quot;&quot; , (max_price,)) >>> c. fetchall () ( ( &quot;Spam and Sausage Lover's Plate&quot; , 5L, 1L, 8L ), ( &quot;Not So Much Spam Plate&quot; , 3L, 2L, 0L ), ( &quot;Don't Wany ANY SPAM! Plate&quot; , 0L, 4L, 3L ) ) >>> c.executemany( ... &quot;&quot;&quot;INSERT INTO breakfast (name, spam, eggs, sausage, price) ... VALUES ( %s , %s , %s , %s , %s )&quot;&quot;&quot; , ... [ ... ( &quot;Spam and Sausage Lover's Plate&quot; , 5, 1, 8, 7.95 ), ... ( &quot;Not So Much Spam Plate&quot; , 3, 2, 0, 3.95 ), ... ( &quot;Don't Wany ANY SPAM! Plate&quot; , 0, 4, 3, 5.95 ) ... ] )‏

Not Good. よろしくないね

よろしくないね

Models Objects Database

Ok, Let's make a model さて、モデルを作りましょう

さて、モデルを作りましょう

Models class Feed (models.Model):     '''A RSS/Atom feed.'''     feed_lifestream = models. ForeignKey (Lifestream)         feed_name = models. CharField ( &quot;Feed Name&quot; , max_length=255)     feed_url = models. URLField ( &quot;Feed Url&quot; , verify_exists=True, max_length=1000)     feed_domain = models. CharField ( &quot;Feed Domain&quot; , max_length=255)     feed_fetchable = models. BooleanField ( &quot;Is Feed Fetchable?&quot; , default=True)       # Used for feeds that allow users to directly add to the lifestream.     feed_basic_feed = models. BooleanField ( &quot;Is Basic Feed?&quot; , default=False)       objects = FeedManager ()       def __unicode__ (self):         return self.feed_name         class Meta :         db_table= &quot;feeds&quot;

class Feed (models.Model):     '''A RSS/Atom feed.'''     feed_lifestream = models. ForeignKey (Lifestream)         feed_name = models. CharField ( &quot;Feed Name&quot; , max_length=255)     feed_url = models. URLField ( &quot;Feed Url&quot; , verify_exists=True, max_length=1000)     feed_domain = models. CharField ( &quot;Feed Domain&quot; , max_length=255)     feed_fetchable = models. BooleanField ( &quot;Is Feed Fetchable?&quot; , default=True)       # Used for feeds that allow users to directly add to the lifestream.     feed_basic_feed = models. BooleanField ( &quot;Is Basic Feed?&quot; , default=False)       objects = FeedManager ()       def __unicode__ (self):         return self.feed_name         class Meta :         db_table= &quot;feeds&quot;

Relationships class Item (models.Model):   '''A feed item'''   # One to many relationship   item_feed = models. ForeignKey ( &quot;Item Feed&quot; , Feed)   item_date = models. DateTimeField ( &quot;Publish Date&quot; )   item_title = models. CharField ( &quot;Title&quot; , max_length=255)   item_content = models. TextField ( &quot;Item Content&quot; , null=True, blank=True)   item_permalink = models. UrlField ( &quot;Permalink&quot; , max_length=1000)‏   # Many to Many relationship   item_tags = models. ManyToManyField (Tag, null=True, blank=True, db_table=&quot;item_tags&quot;)‏   ...

class Item (models.Model):   '''A feed item'''   # One to many relationship

  item_feed = models. ForeignKey ( &quot;Item Feed&quot; , Feed)   item_date = models. DateTimeField ( &quot;Publish Date&quot; )   item_title = models. CharField ( &quot;Title&quot; , max_length=255)   item_content = models. TextField ( &quot;Item Content&quot; , null=True, blank=True)   item_permalink = models. UrlField ( &quot;Permalink&quot; , max_length=1000)‏

  # Many to Many relationship

  item_tags = models. ManyToManyField (Tag, null=True, blank=True, db_table=&quot;item_tags&quot;)‏

  ...

Database Sync [email_address] ~/src/dlife/dlife $ python manage.py syncdb Creating table auth_permission Creating table auth_group Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site Creating table django_admin_log Creating table django_comments Creating table django_comment_flags Creating table lifestream Creating table feeds Creating table tags Creating table items ...

[email_address] ~/src/dlife/dlife $ python manage.py syncdb Creating table auth_permission Creating table auth_group Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site Creating table django_admin_log Creating table django_comments Creating table django_comment_flags Creating table lifestream Creating table feeds Creating table tags Creating table items

...

How do you get at the data? どうやってデータを取れるんですか?

どうやってデータを取れるんですか?

Queries import feedparser   feeds = Feed.objects. filter (feed_basic_feed=False, feed_fetchable=True) for feed in feeds:     d = feedparser. parse (feed.feed_url)‏     for entry in d[ 'entries' ]:         ...         # filters can be chained         items_count = Item.objects. filter (             Q (item_date = date_published) |             Q (item_permalink = permalink)         ). filter (             item_feed = feed         ). count ()‏         ... jQuery ライクなセレクター構文チェーン

import feedparser

 

feeds = Feed.objects. filter (feed_basic_feed=False, feed_fetchable=True) for feed in feeds:

    d = feedparser. parse (feed.feed_url)‏

    for entry in d[ 'entries' ]:

        ...

        # filters can be chained

        items_count = Item.objects. filter (             Q (item_date = date_published) |

            Q (item_permalink = permalink)         ). filter (             item_feed = feed         ). count ()‏

        ...

Queries 2 >>> item = Item.objects. get (id=1)‏ >>> item.item_feed.feed_lifestream.ls_title &quot;Ian's Lifestream&quot; 裏技で全部のオクジェクトを出してくれます。便利 便利

>>> item = Item.objects. get (id=1)‏

>>> item.item_feed.feed_lifestream.ls_title

&quot;Ian's Lifestream&quot;

Saving ... i = Item (item_feed = feed,          item_date = date_published,          item_title = entry. get ( 'title' ),          item_content = content,          item_content_type = content_type,          item_clean_content = clean_content,          item_author = entry. get ( 'author' ),          item_permalink = permalink ) i. save () # を呼び出しておしまい INSERT/UPDATE を調整してくれるから簡単にできる。

...

i = Item (item_feed = feed,          item_date = date_published,          item_title = entry. get ( 'title' ),          item_content = content,          item_content_type = content_type,          item_clean_content = clean_content,          item_author = entry. get ( 'author' ),          item_permalink = permalink ) i. save () # を呼び出しておしまい

Admin モデルから、一般管理ページを生成する機能

モデルから、一般管理ページを生成する機能

Thank you for your listening. ご清聴ありがとうございます。

ご清聴ありがとうございます。

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Models and databases | Django documentation | Django

Contents. Models and databases; Browse. Prev: How to install Django; Next: Models; Table of contents; General Index; Python Module Index; You are here ...
Read more

The Web framework for perfectionists with deadlines | Django

Django takes security seriously and helps developers avoid many common security ... The power of Django. Object-relational mapper; Automatic admin interface;
Read more

LayerMapping data import utility | Django documentation ...

LayerMapping data import utility ... Django automatically logs every SQL query ... (model, data_source, mapping, layer=0, source_srs=None, ...
Read more

Mapping complex python objects to django models - Stack ...

Mapping complex python objects to django ... considering the possibility of actually using django to its full potential and actually using the database ...
Read more

django-feedmapper — django-feedmapper 1.0 documentation

django-feedmapper¶ django-feedmapper is a library for synchronizing data from feeds with Django models. The process of synchronizing the data requires the ...
Read more

Mapping views.py in Django (Python) - Stack Overflow

I need some help mapping views in Django. My app is pretty simple -- it's just a status page listing all of our environments and their corresponding apps.
Read more

Thilo Fromm, 2009

Thilo Fromm, 2009. Videolösungen ... Object-Relational Mapper URL Dispatcher Views, Templating System ... Django OR-mapper
Read more

django-db-multitenant 0.1.1 : Python Package Index

Multitenant support for Django, using one tenant per database. Latest Version: 0.1.2 # django-db-multitenant Provides a simple multi-tenancy solution for ...
Read more

Django - 29C3 Public Wiki - Chaos Computer Club

Django is a high-level Python Web Framework that encourages Rapid Development and ... Some of the key features of Django are: Object-relational mapper (ORM)
Read more