Django for Designers/Adding models: Difference between revisions

m
fixed a minor typo.
imported>Aldeka
imported>Strixcuriosus
m (fixed a minor typo.)
 
(22 intermediate revisions by 2 users not shown)
Line 1:
== Part 3: Models, and our database, and making it visible ==
 
<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 270 ⟶ 272:
</source>
 
If you try to use filter to search for a questionbookmark that does not exist, filter will give you the empty list.
 
<source lang="python">
Line 282 ⟶ 284:
>>> Bookmark.objects.get(id=1)
<Bookmark: http://www.bringinthecats.com/>
>>> Bookmark.objects.get(id=420)
Traceback (most recent call last):
...
Line 290 ⟶ 292:
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 ===
 
=== 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 317 ⟶ 323:
That's our built-in authentication app!
 
LetIf it isn't already open, let's open the Django shell and play with this app a bit.
 
<source lang="bash">
Line 462 ⟶ 468:
<source lang="bash">
# in django-for-designers/myproject
$ cdmkdir bookmarks/templates/registration
# in django-for-designers/myproject/bookmarks/templates
$ mkdir registration
</source>
 
Line 541 ⟶ 545:
Save and commit your changes in adding login/logout functionality.
 
==== Part 3.75: Modify views and templates to use model data ====
 
Let's edit our views and templates so they use real bookmark data from our database!
Line 551 ⟶ 555:
<source lang="bash">
# in django-for-designers/myproject
$ python myproject/manage.py shell
</source>
 
Line 559 ⟶ 563:
 
<source lang="python">
>>> importfrom django.contrib.auth.models import User
>>> me = django.contrib.auth.models.User.objects.all()[0]
</source>
 
Line 568 ⟶ 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 727 ⟶ 731:
{% extends 'index.html' %}
 
{% block subheader %}Bookmarks tagged {{ tag.slug }}{% endblock %}
</source>
 
Line 763 ⟶ 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 774 ⟶ 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]]