如何在 Django 中将对象的字段转换为列表?

问题描述 投票:0回答:1

我有一个使用外键分为不同组(括号)的竞争对手列表。

模型.py

class Group(models.Model):
  group_label = models.CharField(max_length=20, unique=True)
  def __str__(self):
    return self.group_label

class Competitors(models.Model):
  flname = models.CharField(max_length=255)
  Weight = models.IntegerField(validators=[MinValueValidator(50), MaxValueValidator(300)])
  rank = models.CharField(max_length=255)
  Group_FK = models.ForeignKey(Group, blank=True, null=True, on_delete=models.CASCADE, to_field='group_label')
  point = models.IntegerField(default=0)

将竞争对手分配给他们的组后,如何在使用 itertools.combinations() 从中创建唯一对(匹配项)之前将组中的所有 flname 转换为元组?

python django list tuples
1个回答
0
投票

只需使用

values_list
并指定您想要它作为平面列表:

flnames = Competitors.objects.filter(Group_FK=Group.objects.get(pk=___).values_list('flname', flat=True)
© www.soinside.com 2019 - 2024. All rights reserved.