python - SQL命令更新表中的列值

标签 python sql django postgresql

以下是python管理shell上的代码

>>> User.objects.filter(email__icontains="gmail.com").values_list("email", flat=True)
[u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6f6c6d4e69636f6762206d6163" rel="noreferrer noopener nofollow">[email protected]</a>', u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="37415e41525c554456555b5277505a565e5b1954585a" rel="noreferrer noopener nofollow">[email protected]</a>', u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5c3dcc3d0def5d2d8d4dcd99bd6dad8" rel="noreferrer noopener nofollow">[email protected]</a>', u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="68101112280f05090104460b0705" rel="noreferrer noopener nofollow">[email protected]</a>', u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ee8f7e8fbf5fcedfffcf2fbdef9f3fff7f2b0fdf1f3" rel="noreferrer noopener nofollow">[email protected]</a>']
>>> for ii in User.objects.filter(email__icontains="gmail.com"):
...      ii.email = ii.email.replace("@gmail.com", "@custom.com")
...      ii.save() 
...     
... 
>>> User.objects.filter(email__icontains="gmail.com").values_list("email", flat=True)
[]
>>> User.objects.filter(email__icontains="@custom.com").values_list("email", flat=True)
[u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c6d9c6d5dbf0d3c5c3c4dfdd9ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>', u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7afaead97b4a2a4a3b8baf9b4b8ba" rel="noreferrer noopener nofollow">[email protected]</a>', u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cedeeefcceff9fff8e3e1a2efe3e1" rel="noreferrer noopener nofollow">[email protected]</a>', u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46302f30232d243527242a230625333532292b6825292b" rel="noreferrer noopener nofollow">[email protected]</a>', u'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="baccd3ccdfd1d8c9dbd8d6dffad9cfc9ced5d794d9d5d7" rel="noreferrer noopener nofollow">[email protected]</a>']
>>>

我想在Postgresql终端中编写SQL命令(python管理dbshel​​l)

如何在 SQL 命令中转换上述内容?

以下是我的尝试:

[编辑1]:

通过 SQL 命令获取目标电子邮件 ID:

dp=# SELECT  email FROM auth_user where email LIKE '%@gmail.com';
           email           
---------------------------
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="51273827343a11363c30383d7f323e3c" rel="noreferrer noopener nofollow">[email protected]</a>
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6018191a20070d01090c4e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="09686b6a496e64686065276a6664" rel="noreferrer noopener nofollow">[email protected]</a>
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8dfbe4fbe8e6effeecefe1e8cdeae0ece4e1a3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>
 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99eff0effcf2fbeaf8fbf5fcd9fef4f8f0f5b7faf6f4" rel="noreferrer noopener nofollow">[email protected]</a>
(5 rows)

dp=# 

最佳答案

How can I convert above in SQL command?

您可以查看 Django 为此生成的查询,它可能无法运行(例如缺少 Django 发送的参数),但它会让您很好地了解 Django 如何将其转换为 SQL

这个想法是打印这个值:Model.objects.filter(...).values_list(...).query

query = User.objects.filter(email__icontains="@custom.com").values_list("email", flat=True).query

# Make it print it
print query 
print(query) # Python 3 or with "from future import print_function"

关于python - SQL命令更新表中的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437153/

相关文章:

python - 我的 django 应用程序的 settings.py 文件中的这段代码有什么问题?

python - 是什么导致 Python 提示在没有列表的代码中列出列表?

python - 如何将 pandas 数据框合并到现有的 reportlab 表中?

mysql - SQL 加入计数

mysql - 在 SQL 中计算每年的事件数

python - Django 应用程序内存使用情况

python - 如何使用 python 向当前终端历史记录添加命令?

不在路径上的包内的 Python 相对导入

mysql - 如何在 where 子句中包含聚合列?

python - django-graphene 更改列名称弃用