Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Relationships in Django ORM

922 views

Published on

Relationships in Django ORM

Published in: Travel
  • Hello! I can recommend a site that has helped me. It's called ⇒ www.HelpWriting.net ⇐ They helped me for writing my quality research paper.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Relationships in Django ORM

  1. 1. Relationships in Django ORM @starwilly
  2. 2. Outline • Single model ( Django girls tutorial ) • Relationship • 1. many-to-one ( ) • 2. many-to-many (Tag) • What does it looks like in database • How to define relationship • Traverse / query / create / delete relation
  3. 3. A Simple Django Model
  4. 4. CRUD (Create, Read, Update, Delete) Post.objects.create(title='My First Trip', content=‘ ?', location=' ') Post.objects.all() [<Post: My First Trip>, <Post: My Second Trip>, <Post: Django >] Post.objects.filter(pk__gt=1) [<Post: My Second Trip>, <Post: Django >]
  5. 5. CRUD posts = Post.objects.filter(pk__lt=3) [<Post: My First Trip>, <Post: My Second Trip>] posts.update(location=‘ ') 2 posts.delete()
  6. 6. Model : Table id title content photo location created_at 1 … 2 …
  7. 7. Relationship • many-to-one • many-to-many
  8. 8. Many-to-one Relationship
  9. 9. • • • 1. • 2. Alan: Bob: Cindy:
  10. 10. Model
  11. 11. Many-to-one Relationship • PostComment • 1m
  12. 12. ForeignKey • (many-to-one) ForeignKey(othermodel, **options)
  13. 13. Use Foreign Key to Define m:1 Comment Post
  14. 14. id author content 1 Alan … 2 Bob … 3 Cindy … 4 Denny 5 id title content 1 … 2 …
  15. 15. id post_id author content 1 1 Alan … 2 1 Bob … 3 1 Cindy … 4 2 Denny 5 2 Alan id title content 1 … 2 …
  16. 16. Query m:1 Relationship
  17. 17. id = 1 Alan: Bob: Cindy:
  18. 18. Traverse many-to-one relationship id (pk) = 1 post1 = Post.objects.get(pk=1) comments = Comment.objects.filter(post_id=1) comments = Comment.objects.filter(post=post1) [<Comment: Alan: >, <Comment: Bob: >, <Comment: Cindy: >]
  19. 19. Traverse many-to-one relationship id (pk) = 1 post1 = Post.objects.get(pk=1) comments = post1.comment_set.all() <Model>_set [<Comment: Alan: >, <Comment: Bob: >, <Comment: Cindy: >]
  20. 20. Associate Post and Comment post = Post.objects.get(pk=1) comment = Comment(author=‘Billy’, content=‘hi’) post.comment_set.add(comment)
  21. 21. Remove Comment from Post post1 = Post.objects.get(pk=1) post1.comment_set.all() [<Comment: Alan: >, <Comment: Bob: >, <Comment: Cindy: >] c = Comment.objects.get(pk=1) <Comment: Alan: > c.delete() post.comment_set.all() [<Comment: Bob: >, <Comment: Cindy: >]
  22. 22. ForeignKey.related_name comment_list = post.comment_set.all() comment_list = post.comments.all()
  23. 23. Foreign Key • (many-to-one relationship) • <model>_set • related_name • add()
  24. 24. Many-to-many Relationships
  25. 25. Tag myblog.com/post/1 myblog.com/tag/food
  26. 26. What is Many-to-many Relationship Post Tag • tag • tag n m
  27. 27. Tag Model
  28. 28. SlugField • CharField • (hyphen) • • myblog.com/tags/ (X) • myblog.com/tags/food (O) • myblog.com/posts/[ ] (X) • myblog.com/posts/a-wonderful-trip-in-japan (O)
  29. 29. Tag Table id name slug 1 food 2 japan 3 taiwan-north 4 taipei … … taipei
  30. 30. id title content 1 … 2 … Post Tag , , id name slug 1 food 2 japan 3 taiwan-north 4 taipei … …
  31. 31. id title content tags 1 … 1, 3, 4 2 … 2 Post Tag , , id name slug 1 food 2 japan 3 taiwan-north 4 taipei … …
  32. 32. id title content 1 … 2 … Post Tag , , id name slug 1 food 2 japan 3 taiwan-north 4 taipei … … id post_id tag_id 1 1 1 2 2 2 3 1 3 4 1 4
  33. 33. ManyToManyField ManyToManyField(othermodel, **options) tags = models.ManyToManyField('Tag', blank=True)
  34. 34. ManyToManyField • • Django m2m ManyToManyField(othermodel, **options)
  35. 35. id title content 1 … 2 … id name slug 1 food 2 japan … … … id post_id tag_id 1 1 1 2 2 2 3 1 3 4 1 4
  36. 36. Query M2M Relationship from trips.models import Post, Tag post1 = Post.objects.get(pk=1) <Post: > post1.tags.all() [<Tag: >, <Tag: >]
  37. 37. Query M2M Relationship from trips.models import Post, Tag tag_food = Tag.objects.get(pk=1) <Tag: > tag_food.post_set.all() [<Post: >]
  38. 38. ManyToManyField.related_name tag_food = Tag.objects.get(pk=1) tag_food.post_set.all() tag_food.posts.all()
  39. 39. Create M2M Relationship from trips.models import Post, Tag post1 = Post.objects.get(pk=1) tag_japan = Tag.objects.create(name=‘ ’, slug=‘japan’) tag_food = Tag.objects.get(slug=‘food’) post1.tags.add(tag_japan) tag_food.post_set.add(post1)
  40. 40. Remove M2M Relationship from trips.models import Post, Tag tag_japan = Tag.objects.get(slug=‘japan’) post1 = Post.objects.get(pk=1) post1.tags.remove(tag_japan) tag_japan.post_set.remove(post1)
  41. 41. Filter M2M Relationship from trips.models import Post, Tag tag_japan = Tag.objects.get(slug=‘japan’) tag_food = Tag.objects.get(slug=‘food’) tag_japan.post_set.all() # [<Post: >] Post.objects.filter(tags=tag_japan) # [<Post: >] Post.objects.filter(tags__in=[tag_japan, tag_food]) #[<Post: >, <Post: >]
  42. 42. ManyToManyField • • Table (Mapping) • releated_name • add() , remove() • https://docs.djangoproject.com/en/1.9/ref/models/relations/
  43. 43. Get your hand dirty • Tag • Tag • • Tag • Tag Tag
  44. 44. Tips: djangogirls/mysite/templates/post.html
  45. 45. Tips: Tag djangogirls/mysite/templates/post.html
  46. 46. Tips: Tag url(r'^tag/(?P<slug>[w-]+)/$', tag_detail, name='tag_detail'), djangogirls/mysite/trips/views.py djangogirls/mysite/templates/tag.html

×