get_or_create函数比较好用。

如果查询到就返回,如果没查询到就向数据库加入新的对象。

e.g.

size = Size.objects.get_or_create(sizeName=size_text)


注意:返回的是tuple,:(对象, 是否是创建的)

e.g. (size, created)

 

相关文章:

  • 2021-11-08
  • 2021-08-18
  • 2021-11-05
  • 2021-11-26
  • 2021-12-20
  • 2022-12-23
  • 2021-04-05
猜你喜欢
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案