如何加入queryset



我在Django python中使用QuerySet时遇到了一些问题。我有两张桌子/模型。

class PP(models.Model):
imei = models.BigIntegerField(primary_key=True)
name = models.CharField(max_length = 50)
val2 = models.IntegerField(default = 0)
def __str__(self):
return self.name
class Mea(models.Model):
imei = models.ForeignKey(PP, on_delete=models.CASCADE)
v1 = models.IntegerField(default = 0)
v2 = models.IntegerField(default = 0)
v3 = models.IntegerField(default = 0)
dates = models.DateField(default=datetime.date.today)
def __str__(self):
return self.imei
PP
+----+--------+------+
|imei|  name  | val2 |
+----+--------+------+
|  1 | john   |   12 |
|  2 | adam   |    5 |
|  3 | alfred |    3 |
+----+--------+------+

Mea
+----+----+----+-----+---------------------+
|imei| v1 | v2 | v3  |        date         |
+----+----+----+-----+---------------------+
|  1 |  4 | 15 |  18 | 2020-10-16 11:15:53 |
|  1 |  2 | 12 |  17 | 2020-10-16 11:22:53 |
|  1 |  3 | 13 |  16 | 2020-10-16 11:32:53 |
|  2 |  1 | 16 |  15 | 2020-10-16 13:22:53 |
|  2 |  3 | 13 |  25 | 2020-10-16 13:42:53 |
|  2 |  4 | 12 |  35 | 2020-10-16 14:12:53 |
|  3 |  1 | 21 | 12  | 2020-10-16 14:12:53 |
|  3 |  2 | 28 | 42  | 2020-10-16 15:12:53 |
|  3 |  4 | 30 | 72  | 2020-10-16 16:12:53 |
+----+----+----+-----+---------------------+

我需要获得一个表ID,NAME,v1,v2,v3,第一个表中所有对象的新日期像这样的东西:

RESULT
+----+--------+----+----+-----+---------------------+
|imei|  name  | v1 | v2 | v3  |        date         |
+----+--------+----+----+-----+---------------------+
|  1 | john   |  3 | 13 |  16 | 2020-10-16 11:32:53 |
|  2 | adam   |  4 | 12 |  35 | 2020-10-16 14:12:53 |
|  3 | alfred |  4 | 30 | 72  | 2020-10-16 16:12:53 |
+----+--------+----+----+-----+---------------------+

我需要第一张桌子上所有人的第二张桌子上的最新唱片。

可以通过queryset来实现吗?

如果所有PP都有一个或多个相关的mea(即没有null(,那么我认为您可以过滤mea表以排除重复项,并且在没有一个或更多相关mea行的情况下不会错过任何PP行。

qs = Mea.objects.order_by('-dates'      # descending, so newest first
).distinct( 'imei'                   # first row only for each imei 
).order_by( 'imei'                   # re-order if required. 
).select_related('imei'              # for efficiency
)
for mea_instance in qs:
...

来自pp的数据通过mea_instance.imei.name等访问。.select_related('imei')应该缓存其他对象,这样就不会再得到N个DB查询。

向PP模型添加一个新属性,如下所示:

class PP(models.Model):
imei = models.BigIntegerField(primary_key=True)
name = models.CharField(max_length = 50)
val2 = models.IntegerField(default = 0)
@property
def last_mea(self):
return self.mea_set.order_by('date','id').last()
def __str__(self):
return self.name
class Mea(models.Model):
imei = models.ForeignKey(PP, on_delete=models.CASCADE)
v1 = models.IntegerField(default = 0)
v2 = models.IntegerField(default = 0)
v3 = models.IntegerField(default = 0)
dates = models.DateField(default=datetime.date.today)
def __str__(self):
return self.imei

有了这个新属性,您可以访问last_mea doing:

pp.last_mea

因此,在您的模板中,假设PP对象的查询集:

{% for q in queryset %}
<tr>
<td>{{q.imei}}</td>
<td>{{q.name}}</td>
<td>{{q.last_mea.v1}}</td>
<td>{{q.last_mea.v2}}</td>
<td>{{q.last_mea.v3}}</td>
<td>{{q.last_mea.dates | date:'Y-m-d'}}</td>
</tr>
{% endfor %}

编辑:

您不能用以下格式显示时间:;2020-10-16 11:15:53";因为你只存储了一个没有时间的日期。";日期";字段类型为";DateField";而不是";DateTimeField";。如果需要时间,请将DateField更改为DateTimeField和默认值(今天-->现在(。

相关内容

  • 没有找到相关文章

最新更新