我有一个
Session
模型如下:
class Session(models.Model):
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE, related_name="sessions")
flavor = models.ForeignKey(Flavor, null=True, blank=True, on_delete=models.CASCADE, related_name="sessions")
....
我正在尝试运行一个查询:
sessions = Session.objects.all().values('flavor__pk', 'user__pk').distinct()
但是当我打印sessions对象时,我得到了:
<QuerySet [{'user__pk': 14544, 'flavor__pk': 1}, {'user__pk': 14544, 'flavor__pk': 1}, {'user__pk': None, 'flavor__pk': 30}, {'user__pk': 193, 'flavor__pk': 30}, '...(remaining elements truncated)...']>
仔细看,前两个条目是完全相同的
{'user__pk': 14544, 'flavor__pk': 1}