Django创建如果不存在 [英] Django create if doesn't exist

查看:796
本文介绍了Django创建如果不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django应用程序从Web API读取数据并将其放在数据库中。

有没有办法从模式创建一个新的对象,但是如果对象已经存在,则会阻止重复的异常存在?

换句话说,有没有一种保存对象的方法,但是如果它已经存在,只做什么?

解决方案

Model.objects.get_or_create()


I have a Django application that reads data from a web API and puts it in a database.
Is there a way to create a new object from a mode but prevent the duplicate exception if the object already exists?
In other words, is there a way to save an object, but to just do nothing if it already exists?

解决方案

Model.objects.get_or_create()

这篇关于Django创建如果不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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