'Options'对象没有'get_all_related_objects'属性,但我已经在使用Django 1.11了

时间:2017-10-19 19:03:04

标签: python django

我一直在接受 'Options'对象没有属性'get_all_related_objects'错误。我研究过,人们说使用旧版django经常是一个问题,但我使用的是1.11.6

当我导航到url:app / employees时,我收到此错误。

我做错了什么?

Django Version:     1.11.6
Exception Type:     AttributeError
Exception Value:    

'Options' object has no attribute 'get_all_related_objects'

其他版本号:

  • python:2.7.14
  • rest framework:3.1.1
  • virtualenv:12.1.1

应用/型号:

class Employee(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    supervisor = models.ForeignKey('self', blank=True, null=True)
    is_active = models.BooleanField(default=True)
    is_supervisor = models.BooleanField(default=False)

    class Meta:
        ordering = ('last_name',)

    def __str__(self):
        return "{}".format(self.first_name + ' ' + self.last_name)

应用程序/串行化器:

class EmployeeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Employee

应用程序/ api.py:

class EmployeeApi(ListAPIView):
    queryset = Employee.objects.all()
    serializer_class = EmployeeSerializer

应用程序/ url.py

urlpatterns = [
...    
url(r'^employees$', EmployeeApi.as_view()),
]

1 个答案:

答案 0 :(得分:2)

Django v1.11 support is not added for django-rest-framework until version 3.7。升级django-rest-framework应该可以解决问题。

升级django-rest-framework,pip install -U djangorestframework