2017-02-17 55 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 %} 
相關問題