如何在 Django 中使用 .object 查询数据库

Posted

技术标签:

【中文标题】如何在 Django 中使用 .object 查询数据库【英文标题】:How to Query DB using .object in Django 【发布时间】:2017-07-07 02:04:01 【问题描述】:

我的类别很少。说电子产品和玩具。我在商场里有多家商店。商店使用外键(类别)保存。现在在导航栏中.. 我想根据他们的类别列出商店。感谢期待

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 .. 没错。取决于类别选择和类别下的商店列表的下拉列表 【参考方案1】:

要获取所有商店,请使用以下查询。

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

base.html

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

【讨论】:

【参考方案2】:

试试这个:

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 %

 ...

【讨论】:

【参考方案3】:

谢谢各位。我知道这不是最好的方法,但我能够以这种方式解决它

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 %

【讨论】:

以上是关于如何在 Django 中使用 .object 查询数据库的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Django 查询集中使用条件注释 Count

django objects.filter Q() 在一个查询中可以使用多少次?

如何在 django 查询中使用列表 [重复]

如何在 django 中相交两个查询集?

django如何跨表查询

使用 Q-Objects 在 Django 中动态构建复杂查询