gpt4 book ai didi

python - 选择一个有效的选项。 XXXXXXXX 不是可用选项之一

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:51 26 4
gpt4 key购买 nike

我想通过管理员端的多项选择向选定的用户发送短信,但是,在管理员完成选择后,我无法从 select_users 字段中获取值。请帮我解决这个问题。

这是我的代码

表单.py

class SendSMSForm(forms.ModelForm):
select_users = forms.ModelMultipleChoiceField(queryset=SignUp.objects.all(), widget=forms.CheckboxSelectMultiple)
class Meta:
model = SendSMS
fields = ['sms_subject','sms_text']
widgets = {
'sms_text': forms.Textarea()
}
def __init__(self, *args, **kwargs):
super(SendSMSForm, self).__init__(*args, **kwargs)
self.fields['select_users'].choices = [(str(item.mobile_no), str(item.mobile_no)) for item in SignUp.objects.all()]

admin.py

class SendSMSAdmin(admin.ModelAdmin):
change_form_template = "admin/sms/SmsMarketing/change_form.html"
form = SendSMSForm
list_display = ['sms_subject','sms_text','date1']
list_filter = ('sms_subject',)
def get_form(self, request, obj=None, **kwargs):
if request.method == 'POST':
form = SendSMSForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['sms_subject']
message = form.cleaned_data['sms_text']
***users = form.cleaned_data['select_users']***
print users
obj = SendSMS()
for user in users:
result = obj.sendsms(message, user)
obj_name = User.objects.get(username=user)
if result:
SmsHistory.objects.create(
number=user,
recipient=obj_name.get_full_name(),
sms_subject=subject,
sms_text=message,
sms_type = "Promotional SMS"
)
else:
return SendSMSForm
return SendSMSForm

最佳答案

你可以试试:

select_users = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)

关于python - 选择一个有效的选项。 XXXXXXXX 不是可用选项之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37748121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com