DRF:AttributeError:'str'对象没有属性'pk'

时间:2019-03-14 18:13:25

标签: django django-rest-framework attributeerror

我正在使用Django Rest Framework。我有一个TestSuite模型,它与模型Team和EmailTemplates具有外键关系。当我通过邮递员向Test Suite执行post()时,数据将插入到TestSuite表中,并且请求可以成功工作。但是,当我执行get()或尝试单击来自本地主机的URL链接以访问TestSuite时,它会抛出以下错误

 AttributeError at /dqf_api/test_suite/
 'str' object has no attribute 'pk'

错误回溯

File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py" in inner
35.             response = get_response(request)

File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py" in _get_response
128.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py" in _get_response
126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python3.6/contextlib.py" in inner
52.                 return func(*args, **kwds)

File "/usr/local/lib/python3.6/dist-packages/django/views/decorators/csrf.py" in wrapped_view
54.         return view_func(*args, **kwargs)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/viewsets.py" in view
116.             return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py" in dispatch
495.             response = self.handle_exception(exc)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py" in handle_exception
455.             self.raise_uncaught_exception(exc)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py" in dispatch
492.             response = handler(request, *args, **kwargs)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/mixins.py" in list
48.         return Response(serializer.data)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in data
768.         ret = super(ListSerializer, self).data

File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in data
262.                 self._data = self.to_representation(self.instance)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in to_representation
686.             self.child.to_representation(item) for item in iterable

File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in <listcomp>
686.             self.child.to_representation(item) for item in iterable

File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in to_representation
530.                 ret[field.field_name] = field.to_representation(attribute)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/relations.py" in to_representation
272.         return value.pk

Exception Type: AttributeError at /dqf_api/test_suites/
Exception Value: 'str' object has no attribute 'pk'

models.py

class Team(models.Model):
    team_name = models.CharField(primary_key=True, max_length=30)
    description = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'team'

class TestSuite(models.Model):
    team_name = models.ForeignKey(Team, on_delete=models.DO_NOTHING, db_column='team_name')
    suite_name = models.CharField(max_length=100)
    description = models.CharField(max_length=200, blank=True, null=True)
    schedule = models.CharField(max_length=100, blank=True, null=True)
    email_list_ok = models.CharField(max_length=200, blank=True, null=True)
email_list_fail = models.CharField(max_length=200, blank=True, null=True)
template_name = models.ForeignKey(EmailTemplates, on_delete=models.DO_NOTHING, db_column='template_name')

    class Meta:
        managed = False
        db_table = 'test_suite'
        unique_together = (('team_name', 'suite_name'),)

views.py

class TestSuiteViewSet(viewsets.ModelViewSet):
    queryset = models.TestSuite.objects.values('team_name','suite_name', 'description','schedule','email_list_ok','email_list_fail','template_name')
    serializer_class = serializers.TestSuiteSerializer

class TeamViewSet(viewsets.ModelViewSet):
    queryset = models.Team.objects.all()
    serializer_class = serializers.TeamSerializer

serializers.py

class TestSuiteSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.TestSuite
        fields = ['team_name','suite_name', 'description','schedule','email_list_ok','email_list_fail','template_name']

有人可以在这里帮忙吗?我是django和DRF的新手

编辑 当我在serializers.py中使用queryset = models.TestSuite.objects.all()时,它抛出以下错误。

File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py" in _execute
85.                 return self.cursor.execute(sql, params)

File "/usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/base.py" in execute
71.             return self.cursor.execute(query, args)

File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py" in execute
206.         res = self._query(query)

File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py" in _query
312.         db.query(q)

File "/usr/local/lib/python3.6/dist-packages/MySQLdb/connections.py" in query
224.         _mysql.connection.query(self, query)

The above exception ((1054, "Unknown column 'test_suite.id' in 'field list'")) was the direct cause of the following exception:

File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py" in inner
35.             response = get_response(request)

File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py" in _get_response
128.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py" in _get_response
126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python3.6/contextlib.py" in inner
52.                 return func(*args, **kwds)

File "/usr/local/lib/python3.6/dist-packages/django/views/decorators/csrf.py" in wrapped_view
54.         return view_func(*args, **kwargs)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/viewsets.py" in view
116.             return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py" in dispatch
495.             response = self.handle_exception(exc)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py" in handle_exception
455.             self.raise_uncaught_exception(exc)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py" in dispatch
492.             response = handler(request, *args, **kwargs)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/mixins.py" in list
48.         return Response(serializer.data)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in data
768.         ret = super(ListSerializer, self).data

File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in data
262.                 self._data = self.to_representation(self.instance)

File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in to_representation
686.             self.child.to_representation(item) for item in iterable

File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py" in __iter__
272.         self._fetch_all()

File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py" in _fetch_all
1179.             self._result_cache = list(self._iterable_class(self))

File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py" in __iter__
54.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py" in execute_sql
1064.             cursor.execute(sql, params)

File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py" in execute
100.             return super().execute(sql, params)

File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py" in execute
68.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py" in _execute_with_wrappers
77.         return executor(sql, params, many, context)

File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py" in _execute
85.                 return self.cursor.execute(sql, params)

File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py" in __exit__
89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py" in _execute
85.                 return self.cursor.execute(sql, params)

File "/usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/base.py" in execute
71.             return self.cursor.execute(query, args)

File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py" in execute
206.         res = self._query(query)

File "/usr/local/lib/python3.6/dist-packages/MySQLdb/cursors.py" in _query
312.         db.query(q)

File "/usr/local/lib/python3.6/dist-packages/MySQLdb/connections.py" in query
224.         _mysql.connection.query(self, query)

Exception Type: OperationalError at /dqf_api/test_suites/
Exception Value: (1054, "Unknown column 'test_suite.id' in 'field list'")

1 个答案:

答案 0 :(得分:0)

我认为问题出在这里

class TestSuiteViewSet(viewsets.ModelViewSet):
    queryset = models.TestSuite.objects.values('team_name','suite_name', 'description','schedule','email_list_ok','email_list_fail','template_name')  # <-- Here
    serializer_class = serializers.TestSuiteSerializer

您需要提供queryset,而不是dictionary。所以应该像这样:

class TestSuiteViewSet(viewsets.ModelViewSet):
    queryset = models.TestSuite.objects.all()
    serializer_class = serializers.TestSuiteSerializer