Anonymous user
Django for Designers/Adding models: Difference between revisions
m
fixed a minor typo.
imported>Aldeka No edit summary |
imported>Strixcuriosus m (fixed a minor typo.) |
||
(23 intermediate revisions by 2 users not shown) | |||
Line 1:
== Part 3: Models
<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
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
<source lang="python">
Line 282 ⟶ 284:
>>> Bookmark.objects.get(id=1)
<Bookmark: http://www.bringinthecats.com/>
>>> Bookmark.objects.get(id=
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 ==
<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!
<source lang="bash">
Line 462 ⟶ 468:
<source lang="bash">
# in django-for-designers/myproject
$
</source>
Line 541 ⟶ 545:
Save and commit your changes in adding login/logout functionality.
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
</source>
Line 559 ⟶ 563:
<source lang="python">
>>>
>>> me =
</source>
Line 568 ⟶ 572:
<source lang="python">
>>>
>>> import random
>>> tag_names = ['kids', 'read_on_plane', '
>>> # Create the Tag objects
>>> for tag_name in tag_names:
... tag =
... 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 =
... 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(
...
>>> b =
>>> 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 775 ⟶ 779:
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]]
|