使用计算的多对一关系定义Django查询集

时间:2021-06-20 20:27:11

I have three models: Assets, AssetTypes and Services. Assets need to get serviced every n months, and have a many-to-one relation with services.

我有三个模型:资产,资产类型和服务。资产需要每n个月获得一次服务,并与服务有多对一的关系。

class AssetType(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(verbose_name="Asset Type", max_length=100)
    service_period = models.PositiveSmallIntegerField(verbose_name="Service Period (in months)", null=True, blank=True, default=12)

class Asset(models.Model):
    id = models.AutoField(primary_key=True)
    type = models.ForeignKey(AssetType, on_delete=models.PROTECT)

    def service_period(self):
        return AssetType.objects.get(pk=self.type.id).service_period

    def service_history(self):
        return self.service_set.all().order_by('-date')

    def service_due_date(self):
        if self.service_period()==None:
            return None
        elif self.service_history().count()==0:
            return datetime.strptime('2017-01-01', '%Y-%m-%d').date()
        else:
            last_service_date = self.service_history().latest('date').date
            return last_service_date + timedelta(self.service_period()*30)

        def service_overdue(self):
            return ((self.service_due_date()!=None) and self.service_due_date() < date.today())

class Service(models.Model):
    id = models.AutoField(primary_key=True)
    date = models.DateField()
    asset = models.ForeignKey(Asset, on_delete=models.CASCADE)

I'm trying to work out how to make a query set that would return a list of assets that are overdue for their service. I feel like using a model method is a red herring, and that I would be better off defining a query set filter?

我正在尝试研究如何创建一个查询集,该查询集将返回其服务过期的资产列表。我觉得使用模型方法是一个红色的鲱鱼,我最好定义一个查询集过滤器?

I need the list of overdue assets to be a query set so I can use further query set filters on it.

我需要将过期资产列表作为查询集,以便我可以在其上使用更多查询集过滤器。

Any assistance would be greatly appreciated!

任何帮助将不胜感激!

1 个解决方案

#1


0  

So this is a bit tricky.

所以这有点棘手。

To put the query in words, you are looking for all Assets whose latest Service date is earlier than today minus the Type's service period multiplied by 30.

要将查询置于单词中,您要查找其最新服务日期早于今天的所有资产减去类型的服务期间乘以30。

To be honest, I would be tempted to denormalize this; you could add a next_service_due field on Asset which is updated when you add a new Service. Then the query is simply all assets with that field less than today.

说实话,我很想把它归正化;您可以在Asset上添加next_service_due字段,该字段在您添加新服务时会更新。然后查询就是所有具有该字段的资产少于今天。

#1


0  

So this is a bit tricky.

所以这有点棘手。

To put the query in words, you are looking for all Assets whose latest Service date is earlier than today minus the Type's service period multiplied by 30.

要将查询置于单词中,您要查找其最新服务日期早于今天的所有资产减去类型的服务期间乘以30。

To be honest, I would be tempted to denormalize this; you could add a next_service_due field on Asset which is updated when you add a new Service. Then the query is simply all assets with that field less than today.

说实话,我很想把它归正化;您可以在Asset上添加next_service_due字段,该字段在您添加新服务时会更新。然后查询就是所有具有该字段的资产少于今天。