When I use rest_framework to implement the api
models.py:
class Store(models.Model):
name = models.CharField(max_length=20)
notes = models.TextField(blank=True, default='')
def __str__(self):
return self.name
myapp/api.py:
class StoreSerializer(serializers.ModelSerializer):
class Meta:
model = Store
class StoreViewSet(viewsets.ModelViewSet):
queryset = Store.objects.all()
serializer_class = StoreSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
project/api.py
v1 = routers.DefaultRouter()
v1.register('store',StoreViewSet)
v1.register('stores/menu_item',MenuItemViewSet)
urls.py
from .api import v1
urlpatterns = [
url(r'^api/v1/', include(v1.urls)),
]
I met the following traceback
AssertionError at /api/v1/store/ ("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 StoreSerializer serializer.",)
So I add the fields = '__all__'
to fix this error
class StoreSerializer(serializers.ModelSerializer):
fields = '__all__'
class Meta:
model = Store
But next I met this traceback
Based on that traceback I don't know which part is missing.
Put it inside Meta:
class StoreSerializer(serializers.ModelSerializer):
class Meta:
model = Store
fields = '__all__'
specifying-fields
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With