Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

question from:https://stackoverflow.com/questions/65646389/how-to-automatically-count-length-of-total-items-in-django-arrayfield

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
273 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...