2
オブジェクトのセットに.distinct()
を使用する場合は、同じ値から.distinct()
DjangoのORM()
例を使用する目的が損なわれ、何度も繰り返されています私が取り組んでいるプロジェクト:
In [5]: Section.objects.filter(module=15).values('section').distinct()
Out[5]: [{'section': '1'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, {'section': '2'}, '...(remaining elements truncated)...']
なぜ別のものではなく各値を返すのですか?
class Section(models.Model):
module = models.ForeignKey(Module, on_delete=models.CASCADE)
title = models.CharField(max_length=400)
section = models.CharField(max_length=3)
topic = models.CharField(max_length=3)
subtopic = models.CharField(max_length=3)
subsubtopic = models.CharField(max_length=3)
language = models.CharField(max_length=2)
def __str__(self):
return self.sectionid() + " - " + self.title
def sectionid(self):
return self.module.moduleid + ": " + self.justsectionid()
def justsectionid(self):
return self.section + "." + self.topic + "." + self.subtopic + \
self.subsubtopic + "-" + self.language
class Meta:
ordering = ['section', 'topic', 'subtopic', 'subsubtopic', 'language']