Object of type 'QuerySet' is not JSON serializable

Submitted 1 year, 10 months ago
Ticket #26
Views 5214
Language Python
Priority High
Status Closed

ajax code

<button>Load Data</button>
          <hr>
            <script>
             $(document).ready(function(){
                
                $("button").click(function(){
                    
                    $.ajax({
                     url: "loadtabledata", 
                     type: "GET", //send it through get method 
                     data: { year_id : 1 },
                 
                     success: function(response) {
                      
                      $("#loadtable").html(response);
                     },
                     error: function(xhr) {
                      //Do Something to handle error
                      }
                    });
                });
              }); 
            </script> 

urls.py  

path ('loadtabledata',views.loadtabledata,name='loadtabledata'),

class p_board_detail (models.Model):
    
    board_year = models.ForeignKey(p_board_list,on_delete= models.CASCADE)
    member_name = models.CharField(max_length=200)
    member_post = models.CharField(max_length=200)
    
    def __str__(self):
         return "%s (%s) " % (self.board_year,self.member_post)
def loadtabledata(request):
    year_id = request.GET.get('year_id')
    if request.method == 'GET' :
      post_id = p_board_detail.objects.filter(board_year_id= year_id)
      return JsonResponse({'data': post_id})
Submitted on Jul 01, 20
add a comment

1 Answer

Verified

this issue due to you need to serialize the data before send .We have to use method, but as you just started, you can go with the below approach,

post_id = p_board_detail.objects.filter(board_year_id= year_id).values()

return JsonResponse({"data": list(post_id)})

 

Submitted 1 year, 7 months ago

Sorry for that and thansk a lot..

- sarojkoirala42 1 year, 11 months ago


Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message