1

I'm trying to retrieve a list from django class based views and i'm getting the below output in my response

[{},{}]

The model currently has two items in it.

below is my views.py

class CategoryList(generics.ListCreateAPIView):
    renderer_classes = (JSONRenderer,TemplateHTMLRenderer,BrowsableAPIRenderer)
    template_name = 'create.html'
    queryset = Category.objects.all()
    serializer_class = CategorySerializer

models.py

class Category(models.Model):
    category = models.CharField(max_length = 100)

below is my ajax call:

  function getcategories(){
    $.ajax({
      type: 'GET',
      url: 'http://127.0.0.1:8000/billing/categories',
      contentType: 'application/json',
      crossDomain:true,

      success : function(json){
        for (var i = 0; i < json.length; i ++){
        $('#category-row').append('<td>'+json[i].category + '</td>');
        console.log('success');
        console.log(json.length);
      }
    },
      error : function(){
        console.log('there was an error with category-get');
      }
    });

  };

where am I going wrong?

1
  • Post the code for serializer also. Commented Aug 31, 2015 at 16:42

1 Answer 1

1

The problem is probably in your serializer class. It doesn't know how to convert your model into json. It is getting a list of two items, but since it cannot convert them it just puts an empty object.

Sign up to request clarification or add additional context in comments.

1 Comment

Yea .. pretty much spot on .. minor mistake .. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.