Skip to content

Commit

Permalink
Add categories to restaurant page
Browse files Browse the repository at this point in the history
  • Loading branch information
huangsam committed Jul 12, 2020
1 parent 2a23e28 commit ec727ce
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
20 changes: 20 additions & 0 deletions places/migrations/0009_auto_20200712_0155.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.0.8 on 2020-07-12 01:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("places", "0008_category"),
]

operations = [
migrations.AlterField(
model_name="category",
name="places",
field=models.ManyToManyField(
related_name="categories", to="places.Restaurant"
),
),
]
2 changes: 1 addition & 1 deletion places/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __str__(self):

class Category(models.Model):
name = models.CharField(max_length=255)
places = models.ManyToManyField(Restaurant)
places = models.ManyToManyField(Restaurant, related_name="categories")

class Meta:
db_table = "category"
Expand Down
4 changes: 4 additions & 0 deletions places/templates/places/restaurant_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
{% block content %}
<div class="container lead-container pb-3">
<h1>{{ restaurant.name }}</h1>
{% for category in restaurant.categories.all %}
<span class="badge badge-secondary">{{ category.name }}</span>
{% endfor %}
<hr>
<p>Here are some details about the restaurant:</p>
<ul>
<li>Description: {{ restaurant.description }}</li>
Expand Down
8 changes: 8 additions & 0 deletions places/tests/models/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ def test_category_burger(self):
def test_category_earthly(self):
earthly_places = self.earthly_category.places.all()
self.assertEquals(len(earthly_places), 1)

def test_restaurant_five_guys(self):
five_guys = Restaurant.objects.get(name="Five Guys")
self.assertEquals(len(five_guys.categories.all()), 2)

def test_restaurant_in_n_out(self):
in_n_out = Restaurant.objects.get(name="In N Out")
self.assertEquals(len(in_n_out.categories.all()), 1)

0 comments on commit ec727ce

Please sign in to comment.