Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当尝试将django dict转换为json对象时,它会说';str';对象没有属性'_meta&x27&引用;_Python_Ajax_Json_Django - Fatal编程技术网

Python 当尝试将django dict转换为json对象时,它会说';str';对象没有属性'_meta&x27&引用;

Python 当尝试将django dict转换为json对象时,它会说';str';对象没有属性'_meta&x27&引用;,python,ajax,json,django,Python,Ajax,Json,Django,我需要在下拉菜单更改时更新HTML表。我正在使用Ajax来实现这一点。 下面是HTML代码和Ajax代码 HTML代码 <form role="form" action="/surveys/viewsurveys/{{ survey_id }}" method="post" > <div class="form-group">{% csrf_token %} <input type="hidden" id="Surey_ID" name="Surey

我需要在下拉菜单更改时更新HTML表。我正在使用Ajax来实现这一点。 下面是HTML代码和Ajax代码 HTML代码

<form role="form" action="/surveys/viewsurveys/{{ survey_id }}"   method="post" >
  <div class="form-group">{% csrf_token %}
    <input type="hidden" id="Surey_ID" name="Surey_ID" value="{{ survey_id }}">
    <label>Select Department or Section</label>
    <select id="dept" name="dept" class="form-control" onchange="allusers();">
      {% for items in dept_data  %}
         <option value="{{ items.id }}">{{ items.dept_name }}</option>
      {% endfor %}
    </select>
  </div>
 <div class="box-header">
    <h3 class="box-title">All &nbsp<small class="badge pull-right bg-green"> {{ counts.hr_count }}</small></h3>
 </div><!-- /.box-header -->

  <div class="box-body table-responsive">
  <table id="example2" class="table table-bordered table-hover">
      <thead>
        <tr>
           <th> </th>
           <th>First Name</th>
           <th>Last Name</th>
           <th>Email</th>
           <th>Username</th>
           <th>Data join</th>
           <th>last login</th>
           <th>Is Active</th>
        </tr>
      </thead>
      <tbody>
          {% for items in allusers %}
              <tr >
                <td>
                   <div class="input-group">
                       <span class="input-group-addon">
                         <input name="{{ items.id }}" type="checkbox">
                       </span>
                   </div><!-- /input-group -->
                 </td>
                 <td>{{ items.first_name }}</td>
                 <td>{{ items.last_name }}</td>
                 <td>{{ items.email }}</td>
                 <td>{{ items.username }}</td>
                 <td>{{ items.date_join }}</td>
                 <td>{{ items.last_login }}</td>
                 <td>{{ items.is_active }}</td>
                 <td class="text-center"><a class='btn btn-info btn-xs' href="/accounts/editusers/{{ items.id }}"><span class="glyphicon glyphicon-edit"></span> Edit</a>
               </td>
              </tr>
           {% endfor %}   
       </tbody>
           <tfoot>
             <tr>
                <th> </th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Username</th>
                <th>Data join</th>
                <th>last login</th>
                <th>Is Active</th>
              </tr>
            </tfoot>
     </table>
 </div><!-- /.box-body -->
</form>
后端

def ajax_all_dept_users(request):
    try:
        request.session['user_login_data']
        dept_data=SuUserDepartment.objects.filter(org=request.session['user_login_data']['org'])
        dept=request.POST.get('dept','')
        survey_id=request.POST.get('Surey_ID','')
        responce_data={}

        if request.method == 'POST':
            surey_data=SuSurey.objects.get(id=survey_id)
            allusers=SuUser.objects.filter(dept_id=dept)
            responce_data['allusers']=allusers
            responce_data['dept_data']=dept_data
            responce_data['surey_data']=surey_data
            responce_data['survey_id']=survey_id
            data = serializers.serialize('json', responce_data)
            return HttpResponse(json.dumps(data),content_type="application/json")
            #return HttpResponse(json.dumps({'allusers':allusers,'dept_data':dept_data,'surey_data':surey_data, 'survey_id':survey_id }),content_type="application/json")
            #return  render_to_response("pages/forms/publish.html",{'allusers':allusers,'dept_data':dept_data,'surey_data':surey_data, 'survey_id':survey_id },context_instance=RequestContext(request))


    except KeyError, e:
        messages={'alert':'You need to loging'}
        return render(request, 'index.html',{'messages': messages},context_instance=RequestContext(request)) 
我试着调试并试图找出问题所在。我发现当谈到serializers.serialize()时,它会给出这个错误


如何修复此错误?需要快速帮助,这是序列化程序的常见错误。serialize

您正在尝试序列化不是模型查询集的对象

您的数据对象是一个字典,所以您不需要序列化它,只需返回带有json转储的dict即可

responce_data['allusers']=allusers
responce_data['dept_data']=dept_data
responce_data['surey_data']=surey_data
responce_data['survey_id']=survey_id
data = serializers.serialize('json', responce_data)
在这里,您的调用将dict
response\u data
传递给序列化程序,而应该传递要序列化的对象列表

因此,创建对象列表,并且不要在列表中指定要序列化的字符串
survey\u id

将代码更新为

dept_data=list(SuUserDepartment.objects.filter(org=request.session['user_login_data']['org']))

allusers=list(SuUser.objects.filter(dept_id=dept))
responce_data = [ surey_data, ] + dept_data + allusers
data = serializers.serialize('json', responce_data)

我试图得到这个错误'QuerySet'对象没有属性'\u meta'@user3480706,您需要从QuerySet创建列表。请参阅更新的答案以创建列表。它表示“SuSurey”对象不可用iterable@user3480706,您可能想显示更新的代码。我也尝试过,我得到的[,,,]不是JSON Serializable我不知道您在做什么。但是返回HttpResponse(json.dumps({'response_data':response_data}),content_type='application/json;charset=utf8')必须工作。响应_数据必须是字典{key:value}。正如我所说,serializers.serialize用于queryset对象。
dept_data=list(SuUserDepartment.objects.filter(org=request.session['user_login_data']['org']))

allusers=list(SuUser.objects.filter(dept_id=dept))
responce_data = [ surey_data, ] + dept_data + allusers
data = serializers.serialize('json', responce_data)