如果用户没有帐户,如何在10小时后删除数据?

时间:2022-06-06 09:13:51
class Data(models.Model):
    user = models.ForeignKey(User, unique=True)
    other = models.CharField(max_length=255)

class Account(models.Model):
    user = models.ForeignKey(User, unique=True)
    number = models.IntegerField()

How to delete Data after ten hours of added if user do not have an Account

如果用户没有账号,如何在添加10小时后删除数据

2 个解决方案

#1


1  

I used something like this in the past. I would add a "created" field on the Model Data:

我以前用过类似的东西。我将在模型数据上添加一个“创建”字段:

class Data(models.Model):
    user = models.ForeignKey(User, unique=True)
    other = models.CharField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)

And I would set a cron-job, that verifies for Data created without a user association in the last 10 hours.

我将设置一个cron-job,用于验证在过去10个小时内没有用户关联而创建的数据。

#2


1  

You need to add something like:

您需要添加如下内容:

created = models.DateTimeField(auto_now_add=True)

on your model Data, and check it hourly on a cronjob when its past 10 hours of it's creation, in case user is None, delete.

在您的模型数据上,当它在过去的10个小时内创建时,在cronjob上每小时检查一次,以防用户是没有的,删除。

And call them, like in:

打电话给他们,比如:

delete_time = datetime.datetime.now()-datetime.timedelta(hours=10)
to_delete = Data.objects.filter(created__lt=delete_time, user=None).delete()

:)

:)

#1


1  

I used something like this in the past. I would add a "created" field on the Model Data:

我以前用过类似的东西。我将在模型数据上添加一个“创建”字段:

class Data(models.Model):
    user = models.ForeignKey(User, unique=True)
    other = models.CharField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)

And I would set a cron-job, that verifies for Data created without a user association in the last 10 hours.

我将设置一个cron-job,用于验证在过去10个小时内没有用户关联而创建的数据。

#2


1  

You need to add something like:

您需要添加如下内容:

created = models.DateTimeField(auto_now_add=True)

on your model Data, and check it hourly on a cronjob when its past 10 hours of it's creation, in case user is None, delete.

在您的模型数据上,当它在过去的10个小时内创建时,在cronjob上每小时检查一次,以防用户是没有的,删除。

And call them, like in:

打电话给他们,比如:

delete_time = datetime.datetime.now()-datetime.timedelta(hours=10)
to_delete = Data.objects.filter(created__lt=delete_time, user=None).delete()

:)

:)