0
我在Django 1.8得到一個奇怪的錯誤:'下拉列表AutoField' 對象有沒有屬性 'remote_field'
'AutoField' object has no attribute 'remote_field'
我有一個模型,如:
from django.db import models
from django.utils import timezone
class Event(models.Model):
product_type = models.CharField(max_length=250, null=False, blank=False)
received_time = models.DateTimeField(editable=False)
source_json = models.TextField()
event_id = models.CharField(max_length=250, null=False, blank=False)
# https://stackoverflow.com/questions/1737017/django-auto-now-and-auto-now-add
def save(self, *args, **kwargs):
if not self.id:
self.received_time = timezone.now()
return super(Event, self).save(*args, **kwargs)
@classmethod
def event_id_is_already_saved(cls, event_id_in_question):
items_found = cls.objects.filter(event_id=event_id_in_question)
if items_found:
return True
return False
的觀點一樣:
import json
from django.http import HttpResponse
from rest_framework import viewsets
from events.utils.elastic_db_utils import get_elastic_exact_search_from_query_dictionary
from events.serializers import EventSerializer
from events.models import Event
def list_events(request):
all_events = Event.objects.all()
serialized = [EventSerializer(event) for event in all_events]
return HttpResponse(json.dumps(serialized))
class EventViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Event.objects.all().order_by('-received_time')
serializer_class = EventSerializer
網址:
from django.conf.urls import url, include
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'events', views.EventViewSet)
urlpatterns = [
url(r"^$", views.search_elastic_db, name='search-elastic-db'),
url(r"^events", views.list_events, name='list-events'),
url(r'^api/', include(router.urls)),
]
無論是我的視圖集,也不^events
終點的工作,都得到同樣的錯誤。有什麼錯我的模型,你可以在一個shell看到:
In [1]: from events.models import Event
In [2]: Event.objects.all()
Out[2]: [<Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>, <Event: Event object>]
優秀,TY大汗。只要升級到1.9就足夠了,沒有什麼可以打破 – codyc4321