Django - 'RawQuerySet' object has no attribute 'all'
<p>I got this error message if i'm using SQL statement to populate data in dropdown.</p>

<p>Error message</p>

<pre><code>'RawQuerySet' object has no attribute 'all'
</code></pre>

<p>Model.py</p>

<pre><code>@python_2_unicode_compatible  # only if you need to support Python 2
class FacebookAccount(models.Model):
    user = models.ForeignKey(User)
    account_description = models.CharField(max_length=50)
    facebook_application_id = models.CharField(max_length=50)
    facebook_application_secret = models.CharField(max_length=50)
    ouath_token = models.CharField(max_length=500)
    status = models.BooleanField(default=False)

    def __str__(self):
        return self.account_description

@python_2_unicode_compatible  # only if you need to support Python 2
class FacebookFanPage(models.Model):
    facebook_account = models.ForeignKey(FacebookAccount)
    fan_page_description = models.CharField(max_length=50)
    fan_page_id = models.CharField(max_length=30)
    fan_page_access_token = models.CharField(max_length=500, null=True)

    def __str__(self):
        return self.fan_page_description

class Campaign(models.Model):
    aList = (
        ('1', 'Send replies to inbox messages'),
        ('2', 'Post replies to users comments')
    )
    user = models.ForeignKey(User)
    campaign_name = models.CharField(max_length=50)
    autoresponder_type = models.CharField(max_length=10, choices=aList, null=True)
    facebook_account_to_use = models.ForeignKey(FacebookAccount)
    set_auto_reply_for_fan_page = models.ForeignKey(FacebookFanPage)
    message_list_to_use = models.ForeignKey(PredefinedMessage)
    #reply_only_in_this_hourly_interval
    reply_only_for_this_keyword = models.CharField(max_length=50, null=True)
</code></pre>

<p>View.py</p>

<pre><code>def autoresponder_create(request, template_name='autoresponder/autoresponder_form.html'):
if not request.user.is_authenticated():
    return redirect('home')
form = AutoresponderForm(request.POST or None)
form.fields["set_auto_reply_for_fan_page"].query = FacebookFanPage.objects.raw('SELECT * '
                                         'FROM fbautoreply_facebookfanpage '
                                         'JOIN fbautoreply_facebookaccount ON fbautoreply_facebookfanpage.facebook_account_id = fbautoreply_facebookaccount.id '
                                         'WHERE fbautoreply_facebookaccount.user_id = %s ', [str(request.user.id)])
if form.is_valid():
    form = form.save(commit=False)
    form.user = request.user
    form.save()
    return redirect('autoresponder_list')
return render(request, template_name, {'form':form})
</code></pre>

<p>Please advice. Thank you.</p>

<p>As the first comment says it seems like you are calling all() on queryset. You dont have to call .all() after executing raw sql queries, if you are assigning that to a variable since that variable already includes all objects fetched by your query. </p>

<pre><code>In [6]: t = Team.objects.raw('SELECT * FROM core_team')

In [7]: t
Out[7]: &lt;RawQuerySet: SELECT * FROM core_team&gt;

In [8]: t[0]
Out[8]: &lt;Team: test&gt;
In [9]: [x for x in t ]
Out[9]: [&lt;Team: test&gt;, &lt;Team: team2&gt;, &lt;Team: adminTeam&gt;, &lt;Team: team4&gt;]
</code></pre>

<p>and if you call t.all()</p>

<pre><code>In [11]: t.all()
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    &lt;ipython-input-11-2ce0015f044f&gt; in &lt;module&gt;()
    ----&gt; 1 t.all()

    AttributeError: 'RawQuerySet' object has no attribute 'all'

    In [12]: 
</code></pre>

<p>So it seems like you are calling <code>all()</code> on after executing a sql raw query. Remove that piece of code and it will be solved.
 You can refer <a href="https://docs.djangoproject.com/en/1.10/topics/db/sql/#executing-custom-sql-directly" rel="nofollow">to this section of django docs</a> if you want to use better ways to execute a sql query.</p>

<h2>Edit</h2>

<p>Try changing <code>form.fields["set_auto_reply_for_fan_page"].query</code> to <code>form.fields["set_auto_reply_for_fan_page"].queryset</code></p>

python django