Django O/R Mapper

1,364 views

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,364
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Django O/R Mapper

  1. 1. Django O/R Mapper
  2. 2. I'll be giving this presentation in English. <ul><ul><li>このプレゼンを英語でさせて頂きます。 </li></ul></ul>
  3. 3. Just joking. <ul><ul><li>嘘です。 </li></ul></ul>
  4. 4. 自己紹介 <ul><li>名前: イアン・ルイス ( Ian Lewis )‏ </li></ul><ul><li>年齢: 27 歳 </li></ul><ul><li>国籍:米国 </li></ul><ul><li>Twitter: IanMLewis ( 80 %日本語) </li></ul><ul><li>HP: http://www.ianlewis.org/ </li></ul><ul><li>所属 : BeProud </li></ul><ul><li>  </li></ul><ul><li>好きな物: 勉強。挑戦。 Python 。 </li></ul>
  5. 5. Ask me questions anytime. <ul><ul><li>いつでも、質問をしても良い </li></ul></ul>
  6. 6. Why an O/R Mapper? <ul><li>>>> 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 ) ... ] )‏ </li></ul>
  7. 7. Not Good. <ul><ul><li>よろしくないね </li></ul></ul>
  8. 8. Models Objects Database
  9. 9. Ok, Let's make a model <ul><ul><li>さて、モデルを作りましょう </li></ul></ul>
  10. 10. Models <ul><li>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; </li></ul>
  11. 11. Relationships <ul><li>class Item (models.Model):   '''A feed item'''   # One to many relationship </li></ul><ul><li>  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)‏ </li></ul><ul><li>  # Many to Many relationship </li></ul><ul><li>  item_tags = models. ManyToManyField (Tag, null=True, blank=True, db_table=&quot;item_tags&quot;)‏ </li></ul><ul><li>  ... </li></ul>
  12. 12. Database Sync <ul><li>[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 </li></ul><ul><li>... </li></ul>
  13. 13. How do you get at the data? <ul><ul><li>どうやってデータを取れるんですか? </li></ul></ul>
  14. 14. Queries <ul><li>import feedparser </li></ul><ul><li>  </li></ul><ul><li>feeds = Feed.objects. filter (feed_basic_feed=False, feed_fetchable=True) for feed in feeds: </li></ul><ul><li>    d = feedparser. parse (feed.feed_url)‏ </li></ul><ul><li>    for entry in d[ 'entries' ]: </li></ul><ul><li>        ... </li></ul><ul><li>        # filters can be chained </li></ul><ul><li>        items_count = Item.objects. filter (             Q (item_date = date_published) | </li></ul><ul><li>            Q (item_permalink = permalink)         ). filter (             item_feed = feed         ). count ()‏ </li></ul><ul><li>        ... </li></ul>jQuery ライクなセレクター構文チェーン
  15. 15. Queries 2 <ul><li>>>> item = Item.objects. get (id=1)‏ </li></ul><ul><li>>>> item.item_feed.feed_lifestream.ls_title </li></ul><ul><li>&quot;Ian's Lifestream&quot; </li></ul>裏技で全部のオクジェクトを出してくれます。便利 便利
  16. 16. Saving <ul><li>... </li></ul><ul><li>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 () # を呼び出しておしまい </li></ul>INSERT/UPDATE を調整してくれるから簡単にできる。
  17. 17. Admin <ul><li>モデルから、一般管理ページを生成する機能 </li></ul>
  18. 18. Thank you for your listening. <ul><ul><li>ご清聴ありがとうございます。 </li></ul></ul>

×