I'm trying to create a column called stock_count
that counts the total number of items in an ArrayField, in my case the ArrayField is called stock_list
. I've created a function in my model class that doesn't seem to do anything.
class Bucket(models.Model):
class BucketObjects(models.Manager):
def get_queryset(self):
return super().get_queryset()
...
stock_count = models.IntegerField(blank=True, null=True)
stock_list = ArrayField(models.CharField(max_length=6,null=True),size=30,null=True)
objects = models.Manager()
bucketobjects = BucketObjects()
class Meta:
ordering = ('-created',)
def total_stocks_calc(self):
self.stock_count = Bucket.objects.aggregate(Sum('stock_list', distinct=True))
self.save()
I would like this column to be automated, meaning whenever an item is added to an ArrayField, the stock_count
automatically increments by 1. Am I on the right track to create this?