How to update object which was got from django cache

41 views Asked by At

I'm caching my objects, and when I need I'm trying to update value of object which was got from cache. save() method starts work, but doesn't save it, and also doesn't raise any error.

models.py

class AdvertisementQueryset(models.QuerySet):
    def active_advertisements(self) -> models.QuerySet:
        return self.filter(
            status=AdvertisementModelMixin.Statuses.ACTIVE
        )

    def five_low_score_ads(self) -> models.QuerySet:
        if self.all().count() < 5:
            return self.all()
        return (
            self.all()
            .order_by("score")[:5]
        )


class AdvertisementModelMixin(models.Model):
    class Statuses(models.TextChoices):
        ACTIVE = "active", _("Active")
        HIDDEN = "hidden", _("Hidden")
        DELETED = "deleted", _("Deleted")
        TEST = "test", _("Test")

    objects = AdvertisementQueryset.as_manager()

    link = models.URLField()
    view_count = models.PositiveIntegerField(default=0)
    duration = models.PositiveIntegerField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    published_at = models.DateTimeField(blank=True, null=True)
    deleted_at = models.DateTimeField(blank=True, null=True)
    status = models.CharField(
        max_length=30,
        choices=Statuses.choices,
        default=Statuses.ACTIVE
    )
    uuid = models.UUIDField(default=uuid4)
    score = models.IntegerField(default=0)

    @property
    def is_active(self):
        return self.status == self.Statuses.ACTIVE

    @property
    def is_deleted(self):
        return self.status == self.Statuses.DELETED

    @property
    def is_for_test(self):
        return self.status == self.Statuses.TEST

    @property
    def is_hidden(self):
        return self.status == self.Statuses.HIDDEN

    def view_count_increase(self):
        self.view_count += 1
        print("INCREASED")
        self.save()

    def set_as_active(self):
        self.status = self.Statuses.ACTIVE
        self.save()

    def set_as_hidden(self):
        self.status = self.Statuses.HIDDEN
        self.save()

    def set_as_deleted(self):
        self.status = self.Statuses.DELETED
        self.deleted_at = timezone.now()
        self.save()

    def save(self, *args, **kwargs) -> None:
        if not self.published_at and not self.is_hidden:
            self.published_at = timezone.now()

        if not self.deleted_at and self.is_deleted:
            self.deleted_at = timezone.now()

        return super().save(*args, **kwargs)

    class Meta:
        abstract = True

class Banner(AdvertisementModelMixin):
    text = models.CharField(max_length=255)
    description = models.TextField()
    image = WEBPField(
        verbose_name='Image',
        upload_to=banner_folder,
    )

    def __str__(self):
        return f"{self.id}. {self.text}"

    class Meta:
        verbose_name = "banner"

cache.py

def ads_caching(model):
    model_name = model._meta.verbose_name.lower()
    ads_queryset = model.objects.five_low_score_ads()
    ads_list = [ads for ads in ads_queryset]
    cache.set(model_name, ads_list, timeout=None)

utils.py

def ads_data(model, serializer_class):
    model_name = model._meta.verbose_name.lower()
    ads_list = cache.get(model_name)
    queryset = random.choice(ads_list)
    serializer = serializer_class(queryset)
    queryset.view_count_increase()
    return Response(serializer.data)

I want update data in database, not in redis.

0

There are 0 answers