简体   繁体   中英

Django ArrayField append, avoid race condition

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"

实际上,这个解决方案工作正常:

self.my_array_field = Func(F('my_array_field'), Value("foobar"), function='array_append')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM