.update(…) with an extra(…) and F(…)
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'"
.
My first attempt of doing this in Django was:
Model.objects.update(blah='prefix'+F('id')+'suffix')
But that tries to give MySQL a +
, not a ||
operator.
My next attempt was to use the .extra(…)
like so:
Model.objects.extra(select={'newvalue':'"prefix"||id||"suffix"'}).update(blah=F('new_value'))
But the F(…)
is unable to see the new field from the select.
Is there anyway to do this without breaking down to raw SQL?
Nope. Django's ORM is multi db compilant. It is not shipped with custom db features such as this one.
如果我理解你的问题,这可能会有所帮助:
blah = '%s %s %s' %(prefix, id, suffix)
Model.object.update(blah=blah)
链接地址: http://www.djcxy.com/p/38706.html
上一篇: Django筛选任何匹配的列