<p>这是因为在表达式中使用了Python的<code>and</code>关键字,而不是<code>&</code>运算符。</p>
<p>如果<code>a</code>和<code>b</code>都被认为是<code>True</code>,则<code>a and b</code>返回后者,<code>b</code>:</p>
<pre><code>>>> 2 and 3
3
</code></pre>
<p>如果其中任何一个是<code>False</code>,或者两者都是,则返回第一个<code>False</code>对象:</p>
<pre><code>>>> 0 and 3
0
>>> 0 and ''
0
>>>
</code></pre>
<p>一般规则是,<code>and</code>返回第一个允许它决定整个表达式真实性的对象。</p>
<p>在布尔上下文中,Python对象总是被认为是<code>True</code>。所以,你的表情:</p>
<pre><code>Attr("Date").eq(date) and Attr("Shift").eq(shift)
</code></pre>
<p>将计算为最后一个<code>True</code>对象,即:</p>
<pre><code>Attr("Shift").eq(shift)
</code></pre>
<p>这就解释了为什么你只在轮班时过滤。</p>
<p>您需要使用<code>&</code>运算符。在Python中,它通常表示整数之间的“按位和”,它被重新定义为Attr对象表示您想要的内容:“两个条件”。</p>
<p>因此,必须使用“按位和”:</p>
<pre><code>FilterExpression=Attr("Date").eq(date) & Attr("Shift").eq(shift)
</code></pre>
<p>根据<a href="http://boto3.readthedocs.io/en/latest/guide/dynamodb.html#querying-and-scanning" rel="noreferrer">the documentation</a></p>
<blockquote>
<p>You are also able to chain conditions together using the logical
operators: & (and), | (or), and ~ (not).</p>
</blockquote>