I have a model with an ArrayField() and would like to append a value to it.
I can do:
self.my_array_field.append("foobar")
However, this often fails because of race conditions.
So I tried something like this:
self.my_array_field = F('my_array_field') + "foobar"
this:
self.my_array_field = F('my_array_field').append("foobar")
and this:
self.my_array_field = Func(F('my_array_field'), Value("foobar"), function='array_append')
Unfortunately all of these utterly failed.
What would you advise me to achieve this PostGreSQL statement in django?
UPDATE my_model SET my_array_field = array_append(my_array_field, "foobar")
Note that this statement is also equivalent:
UPDATE my_model SET my_array_field = my_array_field || "foobar"
Actually, this solution works fine:
self.my_array_field = Func(F('my_array_field'), Value("foobar"), function='array_append')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With