且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查数据是否存在于django中的***方法是什么?

更新时间:2022-05-07 22:30:52

如果您需要使用

try:
    object = DemoModel.objects.get(id=8)
    #use object here
catch DoesNotExist:
    #catch stuff here

如果不需要,请使用exis ts()。

If you don't need to, just use exists().

if DemoModel.objects.filter(id=8).exists():
    #do stuff here