我想以一种形式显示GraphData模型中存在的每个现有graph_id。像这样:
GRAPHS_CHOICES = (GraphData.objects.all().values_list("graph_id", flat=True).distinct())
class GraphForm(forms.Form):
graphs = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=GRAPHS_CHOICES)
class GraphData(models.Model):
graph_id = models.CharField(max_length=128)
date = models.DateField(max_length=128)
问题在于选择项需要一个元组,而不是ID列表。无论如何,我该如何提供清单?
答案 0 :(得分:0)
您可以将QuerySet更改为此元组
query_tuple = [(q['id'], q['name']) for q in queryset]