查询相关列表中的最后一项

时间:2015-09-08 06:04:38

标签: python django django-queryset

我的model.py看起来像

class Node(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    org_env = models.ForeignKey(OrgEnv)

    name = models.CharField(max_length=100)
    tags = models.ManyToManyField(Tag)

    def __unicode__(self):
        return self.name


class Run(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    node = models.ForeignKey(Node)

    job_id = models.CharField(max_length=100)
    module = models.CharField(max_length=100)
    arguments = models.CharField(max_length=200)
    return_code = models.IntegerField()
    run_time = models.DecimalField(max_digits=10, decimal_places=4)

每个节点可以有很多次运行,我喜欢根据创建日期提取所有节点及其上次运行的查询。

0 个答案:

没有答案