Thursday, February 23, 2012

Is there a orded ModelMultipleChoiceField ?

Hello everyone,
I was writing a website, and meet a problem.
I want users select some instances from a model , and want to print
them in order.
I integrate it with a jquery plugin( http://code.google.com/p/jquery-asmselect/)
,and users can select some instances and they can change the order of
the items. But, the field ModelMultipleChoiceField couldn't take the
order.

Some key codes are:

forms.py
class ExcelForm(forms.Form):
choices =
forms.ModelMultipleChoiceField(queryset=Question.objects.all(),
required=False, widget=forms.SelectMultiple)

views.py
if request.method=='POST':
form = ExcelForm(request.POST)
if form.is_valid():
#print request.POST
wb = Workbook()
sheet = wb.add_sheet(u'test')
print form.data
column = form.cleaned_data['choices']
print column
for i,j in enumerate(column):
sheet.write(0,i,j.text)

datafile = StringIO.StringIO()
wb.save(datafile)
datafile.seek(0)
response = HttpResponse(datafile.read(), mimetype =
'application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment;
filename=test.xls'
return response
else:
form = ExcelForm()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment