Django Query - How to use the objects.filter results
<p>If I use the following code it works perfectly:</p>

<pre><code>campaignnoquery = UserSelection.objects.filter(user=349).order_by('-campaignno')[:1]

for x in campaignnoquery:
    test2 = x.campaignno
</code></pre>

<p>However, when I try:</p>

<pre><code>campaignnoquery = UserSelection.objects.filter(user=349).order_by('-campaignno')[:1]

test1 = campaignnoquery.campaignno
</code></pre>

<p>I get the following error:</p>

<pre><code> test1 = campaignnoquery.campaignno
AttributeError: 'QuerySet' object has no attribute 'campaignno'
</code></pre>

<p>I am sure it is something basic and I could just crack on with the one that worked but I am just intrigued on whats happening.</p>

<p>Many thanks in advance, Alan.</p>

<p><code>campaignnoquery</code> is, as the error says, a <code>QuerySet</code> object (in this case it holds <code>UserSelection</code> instances). </p>

<p>Your error is treating it as an instance of a single <code>UserSelection</code> object.</p>

<p>Are you sure this <code>filter</code> will always return a single object? if so, you can use <code>get</code> instead.</p>

<p>If not, what are you expecting <code>campaignnoquery.campaignno</code> to return? (considering <code>campaignnoquery</code> is a <strong>group</strong> of <code>UserSelection</code> objects).</p>

python mysql django