【发布时间】:2015-02-01 14:38:01
【问题描述】:
错误信息
我刚刚试用了 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'))
)
【问题讨论】:
-
我遇到了这个问题,问题是模型字段与序列化程序的字段不匹配。确保它们在区分大小写方面也都匹配
标签: django django-rest-framework