【发布时间】:2018-10-11 07:25:27
【问题描述】:
测试模型
class Room(models.Model):
"""
This stores details of rooms available
"""
name = models.CharField(
null=False,
blank=False,
max_length=100,
help_text='Enter the name of the room'
)
capacity = models.PositiveIntegerField(
null=False,
blank=False,
help_text='Enter the number of person\'s the room can accommodate'
)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
如果模型存在并且它的 modified_at 时间小于 x 时间,我想更新模型,否则只需创建模型。 作为参考,这里是我希望 django 执行的原始 sql。
INSERT INTO room VALUES (1,'2018-04-30 18:15:32.96468+04:30','2018-04-30 18:15:32.96468+04:30','Room-A',30) ON CONFLICT(id) DO UPDATE SET capacity=10 WHERE room.modified_at < '2017-04-30 18:15:32.96468+04:30';
我也想知道我写的 SQL 查询是否是原子的
【问题讨论】:
标签: django postgresql sql-update