小编kno*_*425的帖子

使用 gin 索引和 sqlalchemy 返回排名搜索结果

我为全文搜索设置了 GIN 索引。我想获取与搜索查询匹配的记录列表,按排名排序(记录与搜索查询的匹配程度)。对于结果,我只需要记录及其列,我不需要用于排序的实际排名值。

我有以下查询,它运行良好并从我的 postgresql 数据库返回预期结果。

SELECT *, ts_rank('{0.1,0.1,0.1,0.1}', users.textsearchable_index_col, to_tsquery('smit:* | ji:*')) AS rank
FROM users
WHERE users.authentication_method != 2 AND users.textsearchable_index_col @@ to_tsquery('smith:* | ji:*') ORDER 
BY rank desc;
Run Code Online (Sandbox Code Playgroud)

我想使用 sqlalchemy(SA) 执行此查询。我了解 'ts_rank' 尚未准备好在 SA 中使用。我尝试了很多东西,例如

proxy = self.db_session.query(User, text(
                """ts_rank('{0.1,0.1,0.1,0.1}', users.textsearchable_index_col, to_tsquery(:search_str1)) as rank""")). \
                filter(User.authentication_method != 2,
                       text("""users.textsearchable_index_col @@ to_tsquery(:search_str2)""")). \
                params(search_str1=search, search_str2=search). \
                order_by("rank")
Run Code Online (Sandbox Code Playgroud)

并阅读有关使用column property 的信息,尽管我不确定是否/如何在解决方案中使用它。

将不胜感激朝正确方向的推动。

python postgresql rdbms full-text-search sqlalchemy

6
推荐指数
1
解决办法
910
查看次数

Django - 将 google recaptcha v2 添加到登录表单

我正在尝试将 Recaptcha 添加到 Django 的登录表单中。我尝试了不同的库,但似乎都不起作用,因为验证码表单没有出现在我的模板中。

这是我目前的工作:

urls.py

path(r'captcha/', include('captcha.urls'))
Run Code Online (Sandbox Code Playgroud)

表格.py

class NewUserForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class YourForm(forms.Form):
        captcha = CaptchaField()

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")

    def save(self, commit=True):
        user = super(NewUserForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        if commit:
            user.save()
        return user
Run Code Online (Sandbox Code Playgroud)

这是我的login.html模板

<form action="/your-name/" method="post">
   {% csrf_token %}
   {{ form.captcha }}
   <input type="submit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,只会Submit出现按钮,而不会出现captcha表单。这就是我尝试过的任何其他库所发生的情况。有人可以给我一些帮助吗?提前致谢!

python django recaptcha

2
推荐指数
1
解决办法
4073
查看次数