django CheckboxSelectMultiple:我的复选框没有被选中
django CheckboxSelectMultiple: my checkboxes are not checked
我有一个 ManyToManyField
,显示有复选框。当我的布尔字段 share_data
等于 1 时,应选中复选框。
代码如下:
class Participant(models.Model):
databases = models.ManyToManyField(Advertiser, null=True, blank=True, through='ShareDataToBrands')
@property
def share_to_brands_list(self):
brands=[]
for brand in ShareDataToBrands.objects.all():
brands.append((brand.advertiser.id, brand.advertiser.brand_name, brand.share_data))
return brands
class ShareDataToBrands(models.Model):
participant = models.ForeignKey(Participant, blank=False, null=False)
advertiser = models.ForeignKey(Advertiser, blank=False, null=False)
share_data= models.BooleanField(default=True)
def __str__(self):
return self.participant, self.advertiser, self.share_data
class ShareDataToBrandsForm(forms.ModelForm):
class Meta:
model = models.Participant
fields = ('databases', )
databases=forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
def __init__(self, *args, **kwargs):
super(ShareDataToBrandsForm, self).__init__(*args, **kwargs)
share_to_brands=self.instance.share_to_brands_list
self.fields['databases'].choices=self.fields['databases'].choices=[(brand[0], brand[1]) for brand in share_to_brands]
self.initial['databases']=[brand[2] for brand in share_to_brands]
print(self.initial['databases'])
for brand in self.instance.share_to_brands_list:
print brand
输出:
[True, True, True]
(1L, u'my super brand', True)
(2L, u'new brand', True)
(3L, u'just my brand', True)
目前,只有第一个复选框被选中,尽管在输出中看到所有复选框都应该被选中。怎么了?
我认为在为 CheckboxSelectMultiple
小部件设置初始值时,您需要传递一个应该检查的 值 列表,而不是一个布尔值列表每个选项。所以做 initial = [True,True,True]
不会让你选中这些框。您想做一些更像 initial = [1,2,3]
.
的事情
在您的示例中,您可以将相关行替换为:
self.initial['databases']=[brand[0] for brand in share_to_brands if brand[2]]
我有一个 ManyToManyField
,显示有复选框。当我的布尔字段 share_data
等于 1 时,应选中复选框。
代码如下:
class Participant(models.Model):
databases = models.ManyToManyField(Advertiser, null=True, blank=True, through='ShareDataToBrands')
@property
def share_to_brands_list(self):
brands=[]
for brand in ShareDataToBrands.objects.all():
brands.append((brand.advertiser.id, brand.advertiser.brand_name, brand.share_data))
return brands
class ShareDataToBrands(models.Model):
participant = models.ForeignKey(Participant, blank=False, null=False)
advertiser = models.ForeignKey(Advertiser, blank=False, null=False)
share_data= models.BooleanField(default=True)
def __str__(self):
return self.participant, self.advertiser, self.share_data
class ShareDataToBrandsForm(forms.ModelForm):
class Meta:
model = models.Participant
fields = ('databases', )
databases=forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
def __init__(self, *args, **kwargs):
super(ShareDataToBrandsForm, self).__init__(*args, **kwargs)
share_to_brands=self.instance.share_to_brands_list
self.fields['databases'].choices=self.fields['databases'].choices=[(brand[0], brand[1]) for brand in share_to_brands]
self.initial['databases']=[brand[2] for brand in share_to_brands]
print(self.initial['databases'])
for brand in self.instance.share_to_brands_list:
print brand
输出:
[True, True, True]
(1L, u'my super brand', True)
(2L, u'new brand', True)
(3L, u'just my brand', True)
目前,只有第一个复选框被选中,尽管在输出中看到所有复选框都应该被选中。怎么了?
我认为在为 CheckboxSelectMultiple
小部件设置初始值时,您需要传递一个应该检查的 值 列表,而不是一个布尔值列表每个选项。所以做 initial = [True,True,True]
不会让你选中这些框。您想做一些更像 initial = [1,2,3]
.
在您的示例中,您可以将相关行替换为:
self.initial['databases']=[brand[0] for brand in share_to_brands if brand[2]]