在django中不是json serializable request.session变量

时间:2016-01-12 21:07:10

标签: json django serialization

我想在django中的request.session变量中存储一些值,但是它会抛出一个错误,说具体的“supplier_value”和“old_validity_date”不是json可序列化的...其他人工作得很好......任何想法? ,这是代码:

class CostItemsSet(View):
    template_name='cost_control_app/cost_item_control.html'

    def get(self, request, *args, **kwargs):
        cost_items = get_object_or_404(CostItems, cost_item = kwargs["cost_item_id"])
        request.session["desc"] = cost_items.description
        request.session["usd"] = cost_items.usd_value
        request.session["rer_value"] = cost_items.rer
        request.session["pesos"] = cost_items.pesos_value
        request.session["supplier_value"] = cost_items.supplier
        request.session["old_validity_date"] = cost_items.validity_date
        form_group = GroupsForm()
        form_cost_items_update= CostItemsForm(
                                        instance =  cost_items,
                                        initial={}
                                        )
        form_subgroup= SubGroupsForm()
        form_cost_item = CostItemsForm()
        form_cost_elements= CostElementsForm()
        form_request= RequestsForm()
        lista = definetree(request.user.groups.all()[0].name, request.user.id)
        return render(request, self.template_name,{
                                                    "form_group" : form_group,
                                                    "form_subgroup" : form_subgroup,
                                                    "form_cost_item" : form_cost_item,
                                                    "form_cost_elements" : form_cost_elements,
                                                    "form_request" : form_request,
                                                    "form_cost_items_update":form_cost_items_update,
                                                    "groups":lista[0],
                                                    "subgroups":lista[1] ,
                                                    "sub_subgroups":lista[2],
                                                    "cost_items":lista[3],
                                                    }) 

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

找到它,转换为字符串完成工作。

感谢所有

答案 1 :(得分:0)

出于某种原因,我遇到了在ugettext _()方法中包含的request.session字符串的问题。将这些字符串包含在str()中(在_()之上)解决了这个问题。