[英]Django list of choices in MultipleChoiceField
我想以一種形式顯示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列表。 無論如何,我如何提供清單?
您可以將QuerySet更改為此
query_tuple = [(q['id'], q['name']) for q in queryset]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.