2017-10-06 47 views
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>] 

回答

1

我不認爲你可以推斷出你所提供的代碼的確切原因,但基於該錯誤消息,似乎很可能你在其他地方使用代碼 - 可能是在第三方庫中 - 這是爲更新版本的Django編寫的。 remote_field屬性被添加到Django v1.9中的Field,如註釋in the release notes。 (此屬性顯然是absent in v1.8。)

的選項有:

  1. 升級您的項目到最新的Django版本。
  2. 反向移植的第三方庫,改變違規Field使用rel,並重寫任何其他不兼容的代碼。
  3. 用不同於支持Django v1.8的第三方庫替換第三方庫。

(我建議第一個選項,因爲Django的版本之間升級爲well-documented

+0

優秀,TY大汗。只要升級到1.9就足夠了,沒有什麼可以打破 – codyc4321