如果我使用SQL语句在下拉列表中填充数据,则会收到此错误消息。
错误消息
'RawQuerySet' object has no attribute 'all'
Model.py
@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)
View.py
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})
答案 0 :(得分:2)
正如第一条评论所说,您似乎在查询集上调用all()。如果要将其分配给变量,则不必在执行原始sql查询后调用.all(),因为该变量已包含查询提取的所有对象。
In [6]: t = Team.objects.raw('SELECT * FROM core_team')
In [7]: t
Out[7]: <RawQuerySet: SELECT * FROM core_team>
In [8]: t[0]
Out[8]: <Team: test>
In [9]: [x for x in t ]
Out[9]: [<Team: test>, <Team: team2>, <Team: adminTeam>, <Team: team4>]
如果你打电话给t.all()
In [11]: t.all()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-2ce0015f044f> in <module>()
----> 1 t.all()
AttributeError: 'RawQuerySet' object has no attribute 'all'
In [12]:
因此,在执行sql raw查询后,您似乎正在调用all()
。删除那段代码,它将被解决。
如果您想使用更好的方法来执行SQL查询,可以参考to this section of django docs。
尝试将form.fields["set_auto_reply_for_fan_page"].query
更改为form.fields["set_auto_reply_for_fan_page"].queryset
答案 1 :(得分:0)
您可以在sql中创建视图或选择查询 然后 您可以使用原始sql操作此查询 例如,您想使过滤器具有类似竞争性。 第一个选择带有视图的所有联接表或选择 查询是第一个查询 新查询是原始处理的查询。 不是:为postgressql选择语法
|> aggregateWindow(every: 15m, fn: count)
答案 2 :(得分:-1)
form.fields["set_auto_reply_for_fan_page"].choices = [(None, '---'),] + [ (x.id, x.name ,) for x in FacebookFanPage.objects.raw('''select id, name from dual''')]