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?
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)
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