gpt4 book ai didi

Django-Rest-Framework 3.0 字段名 '' 对模型 `ModelBase` 无效

转载 作者:行者123 更新时间:2023-12-03 13:29:28 27 4
gpt4 key购买 nike

错误信息

我刚刚试用了 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'))
)

最佳答案

问题发生在 urls.py 下''字段'。确保序列化程序中的字段与 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'

关于Django-Rest-Framework 3.0 字段名 '<field>' 对模型 `ModelBase` 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27281779/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com