Django-Rest-Framework 3.0 Field name '<field>' is not valid for model `ModelBase`
Error message
I just tried out the Django-Rest-Framework 3.0 quick start guide (an excellent introduction by the way) and ran into this error when implementing it on my own system / table.
ImproperlyConfigured at /calls/ Field name `Datecreated` is not valid for model `ModelBase`. I quickly disassembled it and could not find anything, so I wanted to save this solution if someone else (who is also completely new) is faced with the same problem. I pasted the full code, since if you are stuck with this problem, you are probably a beginner and maybe you can use this to see how it all fits.
Table 'CallTraceAttempts'
CallTraceAttemptId DateCreated ... 1 95352 2009-04-10 04:23:58.0000 2 95353 2009-04-10 04:24:08.0000 the code
### models.py in the 'lifeline' app from __future__ import unicode_literals from django.db import models class CallTraceAttempts(models.Model): # Change these fields to your own table columns calltraceattemptid = models.FloatField(db_column='CallTraceAttemptId', blank=True, null=True, primary_key=True) # Field name made lowercase. datecreated = models.DateTimeField(db_column='DateCreated', blank=True, null=True) # Field name made lowercase. class Meta: managed = False # I don't want to create or delete tables db_table = 'CallTraceAttempts' # Change to your own table ### urls.py from django.conf.urls import patterns, include, url from lifeline.models import CallTraceAttempts # Change to your app instead of 'lifeline' from rest_framework import routers, serializers, viewsets # Serializers define the API representation class CallTraceAttemptsSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = CallTraceAttempts fields = ('calltraceattemptid', 'Datecreated') # ViewSets define the view behavior class CallTraceAttemptsViewSet(viewsets.ModelViewSet): queryset = CallTraceAttempts.objects.all() serializer_class = CallTraceAttemptsSerializer # Routers provide an easy way of automatically determining the URL conf router = routers.DefaultRouter() router.register(r'calls', CallTraceAttemptsViewSet) urlpatterns = patterns('', url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ) +7
Will
source share1 answer
Explanation
So the problem arises here under the urls.py fields. Make sure that the fields in your serializer exactly match (case sensitive) with the field on your models.py.
### urls.py #... # Serializers define the API representation class CallTraceAttemptsSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = CallTraceAttempts fields = ('calltraceattemptid', 'datecreated') ### Issue was right here, earlier version had 'Datecreated' +9
Will
source share