Django 坚持 ModelMultipleChoiceField 选择
Django persist ModelMultipleChoiceField selections
我有一个 ModelMultipleChoiceField,允许用户 select 一组或多个我的 "community" 模型(类似于用户加入 subreddit)。当用户重新打开 select 社区页面时,用户之前 select 编辑的字段上没有复选框。我想让以前 selected 的社区保留复选框,因此当用户点击提交时,如果他们不重新 select 以前的选择,他们以前的选择就不会被遗忘.
这是我的表格:
class CustomChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
return obj.name
class CommunitySelectForm(forms.ModelForm):
community_preferences = CustomChoiceField(queryset=Community.objects.all(), widget=forms.CheckboxSelectMultiple)
class Meta:
model= UserQAProfile
fields = ['community_preferences']
这是我的模板:
<div class="col-sm-8 input">
<form method="post" enctype='multipart/form-data'>
{% csrf_token %}
{{ form.as_p }}
<input class="btn btn-submit pull-left" type="submit" value="Choose Communities" />
</form>
</div>
UserQAProfile 模型有一个 ManyToMany 字段来存储首选项:
community_preferences = models.ManyToManyField(Community)
这是初始化表单的视图:
def joinCommunities(request, user_id):
user_ob = get_user_model().objects.filter(id=user_id).first()
full_user_profile = UserQAProfile.objects.filter(user=user_ob).first()
if request.method == 'POST':
form = CommunitySelectForm(request.POST, instance=full_user_profile)
if form.is_valid():
form.save()
context = {'user': full_user_profile, 'full_user_profile':full_user_profile}
context['communities'] = context['user'].community_preferences.all()
return render(request, 'qa/profile.html', context)
else:
form = CommunitySelectForm()
return render(request, 'qa/select_communities.html', {'form' : form})
您仅在 POST
请求期间将 instance
传递给表单,这意味着当用户使用 GET
请求重新访问页面时,表单未绑定到任何实例,之前的选择不会出现。
初始化表单时只需要传实例即可:
else:
form = CommunitySelectForm(instance=full_user_profile)
return render(request, 'qa/select_communities.html', {'form' : form})
我有一个 ModelMultipleChoiceField,允许用户 select 一组或多个我的 "community" 模型(类似于用户加入 subreddit)。当用户重新打开 select 社区页面时,用户之前 select 编辑的字段上没有复选框。我想让以前 selected 的社区保留复选框,因此当用户点击提交时,如果他们不重新 select 以前的选择,他们以前的选择就不会被遗忘.
这是我的表格:
class CustomChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
return obj.name
class CommunitySelectForm(forms.ModelForm):
community_preferences = CustomChoiceField(queryset=Community.objects.all(), widget=forms.CheckboxSelectMultiple)
class Meta:
model= UserQAProfile
fields = ['community_preferences']
这是我的模板:
<div class="col-sm-8 input">
<form method="post" enctype='multipart/form-data'>
{% csrf_token %}
{{ form.as_p }}
<input class="btn btn-submit pull-left" type="submit" value="Choose Communities" />
</form>
</div>
UserQAProfile 模型有一个 ManyToMany 字段来存储首选项:
community_preferences = models.ManyToManyField(Community)
这是初始化表单的视图:
def joinCommunities(request, user_id):
user_ob = get_user_model().objects.filter(id=user_id).first()
full_user_profile = UserQAProfile.objects.filter(user=user_ob).first()
if request.method == 'POST':
form = CommunitySelectForm(request.POST, instance=full_user_profile)
if form.is_valid():
form.save()
context = {'user': full_user_profile, 'full_user_profile':full_user_profile}
context['communities'] = context['user'].community_preferences.all()
return render(request, 'qa/profile.html', context)
else:
form = CommunitySelectForm()
return render(request, 'qa/select_communities.html', {'form' : form})
您仅在 POST
请求期间将 instance
传递给表单,这意味着当用户使用 GET
请求重新访问页面时,表单未绑定到任何实例,之前的选择不会出现。
初始化表单时只需要传实例即可:
else:
form = CommunitySelectForm(instance=full_user_profile)
return render(request, 'qa/select_communities.html', {'form' : form})