Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't update Django ArrayField

Tags:

python

django

I have a class in my model.py as

class Views(models.Model):
    X = ArrayField(models.IntegerField(blank = True))
    Y = ArrayField(models.DecimalField(blank = True,max_digits=2, decimal_places=2))

I entered the default value of X and Y as [0,0,0,0,0,0,0,0,0,0]. But when I try to update the value of X or Y array, it doesn't really get updated.

shell commands are:

>>> from productsHome.models import Views
>>> x = Views.objects.all()
>>> x
<QuerySet [<Views: Views object (5)>]>
>>> x[0]
<Views: Views object (5)>
>>> x[0].X
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> x[0].X = [1,2,2,1]
>>> x[0].X
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>>    

Why isn't my x[0].X is getting updated?

like image 237
ThatMan Avatar asked Sep 19 '25 06:09

ThatMan


1 Answers

Why isn't my x[0].X is getting updated?

Because you make a new query, and thus retrieve the value in the database. In order to update persistently (at the database side), you need to save the object that was updated. For example with:

x = Views.objects.all()
x0 = x[0]
x0.X = [1,2,2,1]
x0.save()

# …
print(x[0].X)
like image 153
Willem Van Onsem Avatar answered Sep 20 '25 20:09

Willem Van Onsem