首页 > 解决方案 > 过滤布尔字段

问题描述

试图过滤布尔字段,但它带来了错误:解包的值太多(预期为 2)这是代码

def index(request):
    if not request.session.has_key('currency'):
        request.session['currency'] = settings.DEFAULT_CURRENCY

    setting = Setting.objects.get(pk=1)
    category = Category.objects.all()
    products_latest = Product.objects.all().order_by('-id')[:4]  # last 4 products
    products_slider = Product.objects.all().order_by('id')[:4]  # first 4 products
    products_picked = Product.objects.all().order_by('?')[:4]  # Random selected 4 products
    products_promoted = Product.objects.filter('promote=True')[:7]  # The error origin

class Product(models.Model):
    title = models.CharField(max_length=150)
    keywords = models.CharField(max_length=255)
    promote = models.BooleanField(default=False)
    description = models.TextField(max_length=255)
    image = models.ImageField(upload_to='images/', null=False)
    price = models.DecimalField(max_digits=12, decimal_places=2, default=0)
    minamount = models.IntegerField(default=3)

为什么会带来错误,我该如何解决?

标签: pythondjango

解决方案


删除'',执行:

products_promoted = Product.objects.filter(promote=True)[:7]

不是:

products_promoted = Product.objects.filter('promote=True')[:7]

推荐阅读