python - 过滤布尔字段
问题描述
试图过滤布尔字段,但它带来了错误:解包的值太多(预期为 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)
为什么会带来错误,我该如何解决?
解决方案
删除''
,执行:
products_promoted = Product.objects.filter(promote=True)[:7]
不是:
products_promoted = Product.objects.filter('promote=True')[:7]
推荐阅读
- cockroachdb - 我可以减少蟑螂集群上非表集群数据的使用吗?
- architecture - 服务的操作可恢复性(?)
- c++ - 是否可以在 C++ 中创建指向类的指针
- c# - 要在 linqToSql C# 中分配 FK 字段和条目,异常:“已尝试附加或添加不是新的实体,....”
- docker - 单节点 Kubernetes 集群纵向扩展
- python - Selentium 找不到元素
- formula - 从今天起检索约会数量加上 7 的正确 Smartsheet 公式是什么
- java - 为什么spring boot vaadin app不能运行?
- angular6 - 在保存织物画布自定义属性时有时会丢失
- r - 从rstudio上传shinyapps的问题