将字符串与CharField与peewee进行比较?

时间:2016-12-16 04:25:12

标签: python sql flask peewee flask-peewee

我试图获取给定字符串address位于models.Listing.address的结果列表。我的代码目前是:

@app.route('/search/<address>')
def search(address):
    results = models.Listing.select().where(address << models.Listing.address)

    return render_template('search.html', results=results)

例如,我可能会传入39 Main Road并希望它选择39 Main Road在给定商家信息的地址字段中的记录(完整地址为39 Main Road RICHMOND, NSW, Australia

但是,我目前的代码错误为:TypeError: unsupported operand type(s) for <<: 'str' and 'CharField'

我尝试将models.Listing.address转换为str,但只返回unsupported operand type(s) for <<: 'str' and 'str'

我会使用address in models.Listing.address,但根据the docsthis answer,使用<<运算符是必要的。

此外,我已尝试使用address.in_(models.Listing.address)哪个字符串没有该属性...

是不是因为我没有将CharField与CharField进行比较?如果是这样,我如何将字符串与CharField进行比较?我已经尝试将CharField更改为字符串,但我不能将in与peewee一起使用,因为@coleifer在this answer的评论中说:

  

Python总是将x in y的返回值强制转换为布尔值,因此需要使用<<运算符。

提前致谢!

1 个答案:

答案 0 :(得分:1)

嗯......我很快就会发现:

.where(models.Listing.address.contains(address))用于检查记录x是否在记录列表或查询中

而我试图实现的功能是通过以下方式完成的:

public void onImageButtonClick(View v) throws InterruptedException { int cardIndex = Integer.parseInt(v.getTag().toString()); ImageButton card = (ImageButton)v; Resources resources = this.getResources(); int imageId = resources.getIdentifier(imageArr[cardIndex-1], "drawable", this.getPackageName()); card.setImageResource(imageId); if(isFirst) { pickedCards[0] = card; } else { pickedCards[1] = card; } PickCard(); } public void PickCard() throws InterruptedException{ try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); r.play(); } catch (Exception e) { Toast.makeText(this, "Can't play music", Toast.LENGTH_LONG); } if(!isFirst) { if (pickedCards[0].getBackground() == pickedCards[1].getBackground()){ Points++; } else { Thread.sleep(1000); pickedCards[0].setImageResource(R.drawable.card); pickedCards[1].setImageResource(R.drawable.card); } } isFirst = !isFirst; }

所以,你去,未来我将不可避免地再次这样做;)