2017-02-17 78 views
0

我有几个类别。说电子产品和玩具。我在商场里有多家商店。一家商店用外键(类别)保存。现在在导航栏中..我想根据他们的类别列出商店。预计感谢如何使用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 %} 
+0

我们需要您指定更多您的需求。我的意思是,你是否需要一个下拉菜单,取决于类别选项,它显示了商店列表? –

+0

@BrianOcampo ..的确如此。下拉菜单取决于类别下的类别选择和商店列表 –

回答

0

要获得所有商店使用以下查询。

shopcat = NewShop.objects.filter(category__category="category name") 

base.html文件

{% for shop in shopcat %} 
    <li><a href="#">{{ shop.name }}</a></l> 
{% endfor %} 
0

试着这样做:

urls.py

urlpatterns = [ 
    ... 

    url(r'^get_shops_by_category/(?P<id_category>\d+)/$', views.get_shops_by_category, name = "get_shops_by_category"), 
] 

views.py

def basefile(request): 
    categories = ShopCategories.objects.all() 
    shop_name = NewShop.objects.filter(name=shop_cat) 
    return render_to_response('base.html', {'Shopname':shop_name, 'categories': categories}) 

def get_shops_by_category(request, **kwargs): 
    categories = ShopCategories.objects.all() 
    current_category = ShopCategories.objects.get(id = kwargs['id_category'] 
    shops = NewShop.objects.filter(category = current_category) 
    return render_to_response('base.html', {'shops': shops, 'categories': categories, 'current_category' current_category}) 

base.html

... 
<select> 
    {% for category in categories %} 
     {% if current_category %} 
      <option value="{{ category.id }}">{{ category.category }}</option> 
     {% else %} 
      <option value="{{ category.id }}" onclick="location.href = '{% url 'your_app:your_url' category.id %}'">{{ category.category }}</option> 
    {% endfor %} 
</select> 

{% if shops %} 
    <table> 
     <thead> 
      <tr> 
       <th>Shop name</th> 
       <th>Actions</th> 
      </tr> 
     </thead> 
     <tbody> 
      {% for shop in shops %} 
       <tr> 
        <td>{{ shop.name }}</td> 
        <td><!-- Buttons or links, ect ... --></td> 
       </tr> 
      {% endfor %} 
     </tbody> 
    </table> 
{% endif %} 

... 
0

谢谢你们。我知道这是不是最好的方法,但是我能解决它这样

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): 
    cat1 = NewShop.objects.filter(category_id=1) 
    cat2 = NewShop.objects.filter(category_id=2) 
    cat3 = NewShop.objects.filter(category_id=3) 
    cat4 = NewShop.objects.filter(category_id=4) 
    shop_name1 = ShopCategories.objects.filter(id=1) 
    shop_name2 = ShopCategories.objects.filter(id=2) 
    shop_name3 = ShopCategories.objects.filter(id=3) 
    shop_name4 = ShopCategories.objects.filter(id=4) 

    return render_to_response('base.html', {'Shop_cat1':cat1, 'Shop_cat2':cat2, 'Shop_cat3':cat3, 
             'Shop_cat4':cat4,'shop_name1':shop_name1, 'shop_name2':shop_name2, 
             'shop_name3':shop_name3, 'shop_name4':shop_name4}) 

base.html文件

{% for shop_name1 in shop_name1 %} 
    <li> 
     <h3> {{ shop_name1 }}</h3> 
    </li> 
{% endfor %} 

{% for Shop_cat1 in Shop_cat1 %} 
    <li><a href="#">{{ Shop_cat1 }}</a></li> 
{% endfor %} 
相关问题