'Is it possible to use f-strings with Concat(), Value() and F()
I have a varchar column whose values I would like to update by concatenating a prefix to a padded integer. Here is what I have tried so far:
Item.objects.update(
field=Concat(
Value('prefix'), Value(f"{F('id'):>011d}")
)
)
Which gives me TypeError: unsupported format string passed to F.__format__
I need help on how I can achieve this if possible.
Solution 1:[1]
Considering the fact that my use case of the f-string was padding, the LPAD and CAST database functions came in handy (I definitely need to study SQL). Here is the update query:
Item.objects.update(
field=Concat(
Value('prefix'), LPad(Cast('id', output_field=CharField()), 11, Value('0'))
)
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eric O. |
