创建Django信号使用Django如何?

huangapple go评论62阅读模式
英文:

Django: how to create a signal using django?

问题

以下是您要翻译的部分:

我有一个竞标系统,其中有一个竞标结束日期,我希望用户能在竞标结束日期之前对自己进行竞标。

产品创建者是那些要添加未来某个日期作为竞标结束日期的人,我该如何编写一个Django信号以检查添加为结束日期的日期是否是当前日期,然后执行一些操作,如将最高竞标者标记为赢家。

这是我的模型:

class Product(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    price = models.DecimalField(max_digits=12, decimal_places=2, default=0.00)
    type = models.CharField(choices=PRODUCT_TYPE, max_length=10, default="regular")
    ending_date = models.DateTimeField(auto_now_add=False, null=True, blank=True)
    ...

class ProductBidders(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True, related_name="product_bidders")
    price = models.DecimalField(max_digits=12, decimal_places=2, default=1.00)
    date = models.DateTimeField(auto_now_add=True)

请注意,我只翻译了您的代码部分。

英文:

I have a bidding system and there is an ending date for the bidding, i want users to bid against themselves till the ending of the bidding date.

The product creators are the ones to add the ending date which would be some date in the future, how can i write a django signal to check if the date that was added as the ending date is the current date, then i'll perform some operations like mark the highest bidder as winner.

This is my model


class Product(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    price = models.DecimalField(max_digits=12, decimal_places=2, default=0.00)
    type = models.CharField(choices=PRODUCT_TYPE, max_length=10, default="regular")
    ending_date = models.DateTimeField(auto_now_add=False, null=True, blank=True)
    ...

class ProductBidders(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True, related_name="product_bidders")
    price = models.DecimalField(max_digits=12, decimal_places=2, default=1.00)
    date = models.DateTimeField(auto_now_add=True)

答案1

得分: 1

我相信仅凭Django信号是无法完成这个任务的。Django信号只能检测到诸如创建实例之类的操作,您可以使用它来调用任务。

您可能需要使用类似Celery的工具来完成这个任务。您可以在创建Product实例时创建一个Celery任务(可以使用'post_save'信号)。在这个时候,您可以传递一个参数告诉Celery何时运行任务。但是,如果用户更改了'ending_date',那么您还需要有一种机制来通知Celery。

另外,您可以运行一个Celery定期任务(使用Celery Beat),定期筛选Product实例以检查'ending_date',如果满足条件,执行所需的操作。

英文:

I believe that you cannot do this with Django signals alone. Django signals have triggers to detect operations like creating an instance, which you can use to invoke a task.

You might have to use something like Celery to do this. You can create a Celery task when you create a Product instance. (For this, you can use 'post_save' signal). At this time, you can pass an argument telling Celery when to run the task. However, if a user changes the 'ending_date', then you would have to have a mechanism to inform that to Celery as well.

Otherwise, you can run a Celery periodic task (using Celery Beat) which would filter Product instances periodically to check the 'ending_date' and perform your required operation if the instance meets the criteria.

huangapple
  • 本文由 发表于 2023年4月17日 08:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定