Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an Assertion error is my django(1.8.4)

from rest_framework import serializers
from .models import Stock


class StockSerializer(serializers.ModelSerializer):

    class Meta:

        model = Stock

        #field = ('ticker','volume')
        field = '__all__'

I am getting an exception value on running application : ("Creating a ModelSerializer without either the 'fields' attribute or the 'exclude' attribute has been deprecated since 3.3.0, and is now disallowed. Add an explicit fields = '__all__' to the StockSerializer serializer.",)

like image 350
Nirdesh Kumar Choudhary Avatar asked Jan 16 '18 13:01

Nirdesh Kumar Choudhary


2 Answers

As the error message says, the required attribute is fields with an 's', not field.

like image 115
Daniel Roseman Avatar answered Nov 08 '22 04:11

Daniel Roseman


I assume that your serializer currently looks like:

class StockerSerializer(serializers.Serializer):
    class Meta:
        model = Stock

The issue Django is complaining about is that the meta class needs to define which fields in the Stock model it needs to serialize. You have three options: you can either put fields = (‘some’, ‘fields’,...), exclude = (‘fields’, ‘other’, ‘than’, ‘these’...), or fields = ‘__all__’.

The easiest option is the last one and it will cause the serializer to serialize all the fields in the Stock model which is probably what you want. You should then revise your code to look like:

class StockerSerializer(serializers.Serializer):
    class Meta:
        model = Stock
        fields = ‘__all__’
like image 7
Rohan Varma Avatar answered Nov 08 '22 03:11

Rohan Varma