信息:我正在尝试制作每月计费应用程序。客户可以按月付款购买房产的地方。如果客户上次付款 30 天前,我想将pending
属性 True 更改为 False。
这是一个基于时间表的应用程序,但我没有使用django-crontab或celerey。如果用户访问页面,我可以使用视图功能,如果日期时间为 30 天pending_customers
,视图可以检查所有客户以及哪些客户最后付款日期。然后视图函数可以更改为 False。created_at
created_at
pending
pending_customers
视图工作正常,但是当我访问该页面时它被更改pending
为 False pending_customers
。它不能等待30天。如果客户最后一次付款不是 30 天,那么我如何才能将查看功能保留到 30 天?
模型.py
class Customer(models.Model):
"""Customer Model"""
name = models.CharField(max_length=255)
prop_select = models.ForeignKey(Property, on_delete=models.SET_NULL, null=True)
created_on = models.DateTimeField(auto_now_add=True)
pending = models.BooleanField(default=False)
class Payment(models.Model):
"""Payment Model"""
customer = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL, related_name='payment')
created_at = models.DateTimeField(auto_now_add=True)
amount = models.DecimalField(max_digits=17, decimal_places=2, default=0.0)
def save(self, *args, **kwargs):
super(Payment, self).save(*args, **kwargs)
self.customer.pending = True
self.customer.save()
视图.py
def pending_customers(request):
queryset = Customer.objects.all()
for i in queryset:
for d in i.payment.filter(created_at__gte=datetime.now() - timedelta(minutes=30)).order_by('-pk')[:1]:
d.customer.pending = False
d.customer.save()
context = {
'customers': queryset,
}
return render(request, 'customer/pending-customers.html', context)