我有两个模型,我尝试按月获取每个模型的对象计数
class Car(models.Model):
car_user = models.ForeignKey(User, on_delete=models.CASCADE)
car_title = models.CharField(max_length=200)
car_purchase_date = models.DateTimeField()
class Truck(models.Model):
truck_user = models.ForeignKey(User, on_delete=models.CASCADE)
truck_title = models.CharField(max_length=200)
truck_purchase_date = models.DateTimeField()
能够得到一个计数的用户与此下面,但无法找出如何做同样的事情,但不是由用户每月做。不会打扰浪费时间和空间与愚蠢的事情,我已经尝试了到目前为止。真的很感激任何帮助。
count_by_user_chart = User.objects.values('username') \
.annotate(car_count=Count('car__id', distinct=True)) \
.annotate(truck_count=Count('truck__id', distinct=True)) \
最终目标是能够生产出这样的东西-
+---------------+-----------+-------------+
| Month | Car Count | Truck Count |
+---------------+-----------+-------------+
| January 2023 | 3 | 1 |
| February 2023 | 4 | 0 |
| March 2023 | 0 | 2 |
| April 2023 | 2 | 2 |
+---------------+-----------+-------------+
1条答案
按热度按时间jdzmm42g1#
以下方法应该有效: