Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 quickstart tutorial (great intro btw) 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 googled it quickly and couldn't find anything so I wanted to save this solution in case someone else (that's also completely new) runs into the same issue. I pasted the full code since if you're stuck on this issue, you're probably new and might be able to use this to see how it all fits together.

Table 'CallTraceAttempts'

     CallTraceAttemptId  DateCreated ...    1              95352  2009-04-10 04:23:58.0000    2              95353  2009-04-10 04:24:08.0000 

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')) ) 
like image 980
Will Avatar asked Dec 03 '14 21:12

Will


1 Answers

The issue happens under urls.py's 'fields'. Make sure that the fields in your serializer match exactly (case sensitive) to the field in 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' 
like image 107
Will Avatar answered Sep 22 '22 03:09

Will