Django ORM-.update(…),带有extra(…)和F(…) [英] Django ORM - .update(…) with an extra(…) and F(…)

查看:297
本文介绍了Django ORM-.update(…),带有extra(…)和F(…)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个SQL查询来更新Django站点中的许多模型。我想更改一个基于ID和一些文本的字符列/字段,在MySQL(此站点为MySQL)中,我可以使用 UPDATE table SET blah ='prefix'| | id ||后缀 c。

I want to do one sql query to update a lot of models in a Django site. I want to change one char column/field to be based on the id and some text, in MySQL (which this site is), I'd do that with "UPDATE table SET blah = 'prefix'||id||'suffix'".

我在Django中进行此操作的首次尝试是:

My first attempt of doing this in Django was:

Model.objects.update(blah='prefix'+F('id')+'suffix')

但这试图给MySQL一个 + ,而不是一个 || 运算符。

But that tries to give MySQL a +, not a || operator.

我的下一个尝试是使用 .extra(…)

My next attempt was to use the .extra(…) like so:

Model.objects.extra(select={'newvalue':'"prefix"||id||"suffix"'}).update(blah=F('new_value'))

但是 F(…)无法从选择中看到新字段。

But the F(…) is unable to see the new field from the select.

无论如何,都可以做到这一点而不会分解为原始数据SQL?

Is there anyway to do this without breaking down to raw SQL?

推荐答案

不是。 Django的ORM是多数据库编译器。它未附带诸如此类的自定义数据库功能。

Nope. Django's ORM is multi db compilant. It is not shipped with custom db features such as this one.

这篇关于Django ORM-.update(…),带有extra(…)和F(…)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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