我正在尝试为相应的餐厅提供所有菜肴,但它一直在展示餐厅.FoodItem.None
Models.py:
class Restaurant(models.Model):
name = models.CharField(max_length = 200)
email = models.EmailField(max_length = 254)
address = models.CharField(max_length=100)
city = models.CharField(max_length=100)
website = models.TextField(validators=[URLValidator()])
telephone = models.PositiveIntegerField()
owner = models.ForeignKey(Owner , on_delete = models.CASCADE)
def __str__(self):
return self.name
class FoodItem(models.Model):
name = models.CharField(max_length = 200)
price = models.DecimalField(max_digits=8, decimal_places=2, blank=True,
null=True)
description = models.TextField(blank=True, null=True)
restaurant = models.ForeignKey('restaurant.Restaurant', null=True,
related_name='dishes' , on_delete = models.CASCADE)
def __str__(self):
return self.name;
views.py:
class FoodItemDetailView(DetailView):
model = Restaurant
def get_context_data(self , *args , **kwargs):
context = super(FoodItemDetailView , self).get_context_data(*args , **kwargs)
print(context)
return context
restaurant_detail.html: 在下面没有菜的名称被打印,但餐厅的名称正在打印
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="row">
<div class="col col-sm-6 ">
restaurant and serving food item
{{ object.name }}
{% for dish in object.dishes.all %}
{{ dish.name }}
{% endfor %}
</div>
</div>
</div>
{% endblock %}