django/query.py at master · django/django https://github.com/django/django/blob/master/django/db/models/query.py


def get_or_create(self, defaults=None, **kwargs):
"""
Look up an object with the given kwargs, creating one if necessary.
Return a tuple of (object, created), where created is a boolean
specifying whether an object was created.
"""
# The get() needs to be targeted at the write database in order
# to avoid potential transaction consistency problems.
self._for_write = True
try:
return self.get(**kwargs), False
except self.model.DoesNotExist:
params = self._extract_model_params(defaults, **kwargs)
return self._create_object_from_params(kwargs, params)

def update_or_create(self, defaults=None, **kwargs):
"""
Look up an object with the given kwargs, updating one with defaults
if it exists, otherwise create a new one.
Return a tuple (object, created), where created is a boolean
specifying whether an object was created.
"""
defaults = defaults or {}
self._for_write = True
with transaction.atomic(using=self.db):
try:
obj = self.select_for_update().get(**kwargs)
except self.model.DoesNotExist:
params = self._extract_model_params(defaults, **kwargs)
# Lock the row so that a concurrent update is blocked until
# after update_or_create() has performed its save.
obj, created = self._create_object_from_params(kwargs, params, lock=True)
if created:
return obj, created
for k, v in resolve_callables(defaults):
setattr(obj, k, v)
obj.save(using=self.db)
return obj, False

get_or_create update_or_create的更多相关文章

  1. django的数据库操作

    ORM是通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到关系数据库中,ORM在业务逻辑层和数据库层之间充当了桥梁的作用. django的交互式shell python manage ...

  2. [py]django强悍的数据库接口(QuerySet API)-增删改查

    django强悍的数据库接口(QuerySet API) 4种方法插入数据 获取某个对象 filter过滤符合条件的对象 filter过滤排除某条件的对象- 支持链式多重查询 没找到排序的 - 4种方 ...

  3. Python全栈开发之21、django

    http://www.cnblogs.com/wupeiqi/articles/5237704.html http://www.cnblogs.com/wupeiqi/articles/5246483 ...

  4. 沉淀,再出发:Django的简单使用

    沉淀,再出发:Django的简单使用 一.前言     在学习了python的基础语法之后,其实大家都很怀疑python的使用场景,其实python在很多场合都有很强的适应性,就比如说web开发之中使 ...

  5. web框架详解之三Modal

    一.Modal操作之创建表,添加数据 1. 配置Django中settings的设置连接mysql数据库,然后在mysql数据库中创建库 2. 在models中创建表.继承Model 3. 在sett ...

  6. [TimLinux] django model关于QuerySet

    1. 获取执行过的sql命令 from django.db import connections connections['default'].queries 2. 获取QuerySet将执行的sql ...

  7. Django之queryset API

    1. QuerySet 创建对象的方法 >>> from blog.models import Blog >>> b = Blog(name='Beatles Bl ...

  8. django - get_or_create() 使用提醒

    [omron - debug] user_id建表的时候,不能使用unique,因为一个用户,可能有多个product_id,相对应的是,get_or_create()中的查询参数,如果在建表中有un ...

  9. django ORM中update_or_create功能,如果只要匹配某一特定字段呢

    今天发现的需求,在官方文档找到说法: In English, that means start with any non-'defaults' keyword argument that doesn’ ...

随机推荐

  1. EF中使用UnitOfWork

    前言 关于EF5中使用UnitWork,参见另一博文:  https://www.cnblogs.com/masonblog/p/9801162.html 每次提交数据库都会打开一个连接,造成结果是: ...

  2. [.NET] - EventSource类的使用

    EventSource类: 这个类是在.NET 4.5新推出的一个类,用来提供创建事件用于 Windows 事件跟踪的功能 (ETW).在之前如果要配置一个Event Tracing for Wind ...

  3. [论文阅读笔记] metapath2vec: Scalable Representation Learning for Heterogeneous Networks

    [论文阅读笔记] metapath2vec: Scalable Representation Learning for Heterogeneous Networks 本文结构 解决问题 主要贡献 算法 ...

  4. 一个简单的java项目使用hibernate连接mysql数据库

    实体类与表对应文件Customer.hbm.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTY ...

  5. [leetcode]21Merge Sorted ListNode递归合并顺序链表

    /** * Merge two sorted linked lists and return it as a new list. * The new list should be made by sp ...

  6. JDBC访问数据库的基本步骤是什么?

    1.加载(注册)数据库驱动(到JVM) 2.建立(获取)数据库连接. 3.创建(获取)数据库操作对象. 4.定义操作的SQL语句. 5.执行数据库操作. 6.获取并操作结果集. 7.关闭对象,回收数据 ...

  7. 虚拟机安装Ubuntu 16.04系统实操教程 详尽步骤 vmware ESXi亲测通过

    1 Ubuntu 16.04系统安装要求 Ubuntu 16.04 LTS下载最新版本的Ubuntu,适用于台式机和笔记本电脑. LTS代表长期支持,这意味着有五年免费安全和维护更新的保证. Ubun ...

  8. ubuntu虚拟机启用双网卡IP配置

    首先要登入自己的虚拟机,这里以ubuntu为例. 配置两块网卡,一块eth0为NAT模式,另一块为eth1仅主机模式 # 进入网卡配置页面vi /etc/network/interfaces # Th ...

  9. SpringSecurity配置文件

    @EnableWebSecurity public class seccurityConfig extends WebSecurityConfigurerAdapter { @Override pro ...

  10. SpringBoot官网提供所有组件整理

    下面所有SpringBoot组件整理来自于:https://start.spring.io/,紧随Spring社区的步伐...... Developer Tools Spring Boot DevTo ...