Django for Designers/Adding models: Difference between revisions

m
fixed a minor typo.
imported>Aldeka
imported>Strixcuriosus
m (fixed a minor typo.)
 
(11 intermediate revisions by 2 users not shown)
Line 1:
== Part 3: Models and our database ==
 
<div class="instructor">Time: 50 minutes</div>
 
Remember to make a new branch for section 3 based off the official branch!
Line 65 ⟶ 67:
<div class="instructor">The tag slug is stored in another CharField, which we've seen before. But what's this ManyToManyField? Well, it's one type of field for denoting a relationship between two models.
 
In relational databases, there's are two basic types of relationships. In a ForeignKey relationship, one model relates to one and only one other model. You could imagine this like a Car model and a Wheel model. Each Wheel instance belongs to one and only one Car, so it would have a ForeignKey field for its car. The car, of course, can have multiple wheels.
 
With a ManyToManyField, on the other hand, the relationship isn't exclusive for either of the models involved. For instance, you could imagine having a Pizza model and a Topping model. Each Pizza can have multiple Toppings, and each Topping can be on multiple Pizzas.
Line 289 ⟶ 291:
 
More information on making queries with Django's ORM can be found in the Django docs at https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-objects.
 
<div class="instructor">Go take a break!!!</div>
 
== Part 3.5: Changing our mind and adding users ==
 
<div class="instructor">Time: 1 hour 35 minutes</div>
 
<div class="instructor">D'oh! You know what every social bookmarking app has, that ours doesn't have? Users!
Line 566 ⟶ 572:
 
<source lang="python">
>>> importfrom bookmarks.models import Bookmark, Tag
>>> import random
>>> tag_names = ['kids', 'read_on_plane', 'send_to_momsend_to_dad']
>>> # Create the Tag objects
>>> for tag_name in tag_names:
... tag = bookmarks.models.Tag(slug=tag_name)
... tag.save()
...
>>> kids_urls = ['http://pbskids.org/', 'http://www.clubpenguin.com/', 'http://www.aplusmath.com/hh/index.html', 'http://kids.nationalgeographic.com/kids/activities/recipes/lucky-smoothie/', 'http://www.handwritingforkids.com/', 'http://pinterest.com/catfrilda/origami-for-kids/', 'http://richkidsofinstagram.tumblr.com/', 'http://www.dorkly.com/picture/50768/', 'http://www.whyzz.com/what-is-paint-made-of', 'http://yahooligans.com/']
>>> for kids_url in kids_urls:
... b = bookmarks.models.Bookmark(author=me, url=kids_url)
... b.save()
... how_many_tags = random.randrange(len(tag_names))
... for tag_name in random.sample(tag_names, how_many_tags):
... b.tag_set.add(bookmarks.models.Tag.objects.get(slug=tag_name))
...
>>> b = bookmarks.models.Bookmark.objects.get(url='http://yahooligans.com/')
>>> b.title = "Yahoooo!!!!!ligans"
>>> b.save()
Line 725 ⟶ 731:
{% extends 'index.html' %}
 
{% block subheader %}Bookmarks tagged {{ tag.slug }}{% endblock %}
</source>
 
Line 761 ⟶ 767:
Save and commit your error-catching work.
 
<!-- ==== Let me show off ====
 
So far, we've all only been able to access our own Django-powered sites. In this section, you will see how to access my (the instructor's) super cool bookmarks site! (We'll talk later about how you can share your code the same way. It might require firewall configuration changes on your computer, so we save that complexity for later.)
Line 772 ⟶ 778:
 
When you visit the instructor's app, you are interacting with the database stored on her laptop. So all the changes made by other people in the room are reflected in what you see! So try not to be too silly.
 
No demo for now.
 
-->
 
<!-- Instructor git note: git push origin HEAD:pre-part-4 00-->
 
[[Django_for_Designers/CRUD|Next page]]