2014-12-03 14 views
7

Komunikat o błędzieDjango-Rest-Framework 3.0 Pole nazwa '<field>' nie jest ważne dla modelu `ModelBase`

Właśnie wypraktykowałem Django-Rest-Framework 3.0 quickstart tutorial (świetny wstęp btw) i wpadł ten błąd podczas implementacji go na moim własnym systemie/tabeli.

ImproperlyConfigured at /calls/ 
Field name `Datecreated` is not valid for model `ModelBase`. 

google go szybko i nie mógł znaleźć nic więc chciałem zapisać to rozwiązanie w przypadku kogoś innego (to również zupełnie nowa) prowadzi do tego samego problemu. Wkleiłem cały kod, ponieważ jeśli utkniesz w tym wydaniu, prawdopodobnie jesteś nowy i możesz go użyć, aby zobaczyć, jak to wszystko razem pasuje.

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

Kod

tabeli 'CallTraceAttempts'

### 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')) 
) 

Odpowiedz

9

Wyjaśnienie

Więc problem występuje tutaj w 'polach' urls.py użytkownika. Upewnij się, że pola w serializatorze dokładnie pasują (z uwzględnieniem wielkości liter) do pola w pliku 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' 
Powiązane problemy