我有几个类别。说电子产品和玩具。我在商场里有多家商店。一家商店用外键(类别)保存。现在在导航栏中..我想根据他们的类别列出商店。预计感谢如何使用Django中的.object查询数据库
models.py
class ShopCategories(models.Model):
category = models.CharField(max_length=50, unique=True,)
def __str__(self):
return self.category
class NewShop(models.Model):
category = models.ForeignKey(ShopCategories)
name = models.CharField(max_length=100, unique=True)
tagline = models.CharField(max_length=50, default='Enter tagline here2')
description = models.TextField(default='enter shop description')
def __str__(self):
return self.name
views.py
def basefile(request):
shop_cat = NewShop.objects.filter(category_id=1)
shop_name = NewShop.objects.filter(name=shop_cat)
return render_to_response('base.html', {'Shopname':shop_name, 'Shopcat':shop_cat})
base.html
{% for category_id in Shopcat %}
<li><a href="#">{{ Shopname }}</a></l>
{% endfor %}
我们需要您指定更多您的需求。我的意思是,你是否需要一个下拉菜单,取决于类别选项,它显示了商店列表? –
@BrianOcampo ..的确如此。下拉菜单取决于类别下的类别选择和商店列表 –