如何从更改列表中获取所选项目?

时间:2014-02-28 16:50:28

标签: python django

我是python / django的新手,并编写了一个名为List的自定义动作,其中我显示了一个显示数据的html表。 如果选择了某些内容但它显示了所有项目,即使我只选择了其中一些项目,它也能正常工作。 我试图只显示我在html表中选择的项目,但无法找到如何做到这一点。 我的模型称为Exchangerate,并将currencycodefrom,currencycodeto,effectivedate(PK)和conversionratemultiplier作为字段。 这是ExchangerateAdmin:

class ExchangerateAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        post = request.POST.copy()
        post.setlist(admin.helpers.ACTION_CHECKBOX_NAME,
        self.model.objects.values_list('effectivedate', flat=True))
        request.POST = post
        #qs = self.model._default_manager.get_queryset()
        qs = super(ExchangerateAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(editors=request.user)
    actions = [export_csv,list,retrieve,]

这是我的列表操作代码:

def list(self, request, queryset):
from django.utils.encoding import smart_str

queryset = ExchangerateAdmin.get_queryset(self, request)
serializer = ExchangerateSerializer(queryset, many=True)
res = "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"C:/Django/django-django-2dcde52/django/contrib/admin/static/admin/css/donationsdue.css\" /></head>"
res += "<body bgcolor=\"#9E9E8A\" ><H1 style=\"color:#275858;text-align:center\">Exchange Rates</H1><br><table border='10' align=\"center\">"
res += "<tr><th  bgcolor=\"#4FDB00\">Currency To</th><th  bgcolor=\"#4FDB00\">Currency From</th><th  bgcolor=\"#4FDB00\">Effective Date YYYY-MM-DD</th><th  bgcolor=\"#4FDB00\">Conversion Multiplier</th></tr>"
count = 0
for item in serializer.data:
    thisitem = str(item)
    thisitem = thisitem[1:len(thisitem)-1]
    res += "<tr  bgcolor=\"#CEFF00\">"
    splitlist = thisitem.split("\n",)
    print splitlist[0]
    for line in splitlist:
        splitlist2 = str(line).split(":",)
        for line2 in splitlist2:
            if 'datetime.date'in line2:
                datestr = str(line2)[14:len(str(line2))]
                datestr = datestr[1:len(str(line2))-1]
                datelist = datestr.split(",",)
            splitlist3 = str(line2).split(",",)
            print splitlist3[0]

            print len(splitlist3[0])
            if 'u' in splitlist3[0]:
                splitlist3[0]=splitlist3[0][2:len(splitlist3[0])]    
            if 'datetime.date'in splitlist3[0]:
                splitlist3[0]=splitlist3[0][14:len(splitlist3[0])] 
                splitlist3[0]=splitlist3[0]+'-'+datelist[1].replace(' ','')+'-'+datelist[2].replace(' ','')
                splitlist3[0]=splitlist3[0][1:len(splitlist3[0])-1]
            if 'Decimal' in splitlist3[0]:
                splitlist3[0]=splitlist3[0][7:len(splitlist3[0])]
                splitlist3[0]=splitlist3[0][3:len(splitlist3[0])-2]
            if count%5>0:
                res+="<td>"+splitlist3[0]+"</td>"
            count+=1
            #res+="<td>"+smart_str(line)+"</td>"
    res+="</tr>"
res+="</body></table><input type=\"button\" value=\"Previous Page\" onClick=\"javascript:history.go(-1);\" />"
return HttpResponse(res)

我已经用Google搜索了几个星期而且无法做到这一点。 谁能让我走上正确的道路?

0 个答案:

没有答案