错误消息
我刚尝试了Django-Rest-Framework 3.0 quickstart tutorial(很好的介绍顺便说一句),并在我自己的系统/表上实现时遇到了这个错误。
ImproperlyConfigured at /calls/
Field name `Datecreated` is not valid for model `ModelBase`.
我快速搜索了它,找不到任何内容,所以我想保存此解决方案以防其他人(也是全新的)遇到同样的问题。我粘贴了完整的代码,因为如果你坚持这个问题,你可能是新的,也许可以使用它来看看它们是如何组合在一起的。
表'CallTraceAttempts'
CallTraceAttemptId DateCreated ...
1 95352 2009-04-10 04:23:58.0000
2 95353 2009-04-10 04:24:08.0000
代码
### 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'))
)
答案 0 :(得分:18)
<强>解释强>
所以这个问题发生在urls.py的'fields'下面。确保序列化程序中的字段与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'
答案 1 :(得分:2)
class CallTraceAttemptsSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = CallTraceAttempts
fields = '__all__'
将字段值更改为'all',它需要所有字段值