如何检查在postgresql数据库中是否存在使用django? [英] How to check if something exists in a postgresql database using django?

查看:112
本文介绍了如何检查在postgresql数据库中是否存在使用django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查数据库中的行是否已经包含特定的输入。如果它已经存在,防止它被再次添加,如果没有,然后添加它像正常。

I want to check to see if row in the database already contains a particular input. If it does already exist, prevent it from being added again, if not then add it like normal.

如何查询数据库中是否存在某些内容,而无需将所有内容拉出数据库,以便检查?

How can I ask the database if something exists without pulling all of the contents out of the database in order to check?

推荐答案

您可以使用

Entry.objects.filter(name='name', title='title').exists()

值。当使用count时,orm生成的查询将执行比exists方法更长的时间。当对象不存在时,get方法将引发一个异常。

This will return to you true/false values. When you use count the orm generates query which will be executed much longer than in exists method. The get method will raise an exception when object does not exists.

request.POST是一个字典,所以要使用它检查db,即:

request.POST is a dictionary so to check db with it you use, i.e.:

Entry.objects.filter(name=request.POST['name'], title=request.POST['title']).exists()

这篇关于如何检查在postgresql数据库中是否存在使用django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆