Django rest框架:AttributeError:'NoneType'对象没有属性'_meta'

时间:2019-02-17 18:39:02

标签: django django-rest-framework

我正在youtube上关注本教程: link

为了确定,我什至第三次从他的github复制了准确​​的代码,但是我仍然遇到此错误,

我正在使用rest api,根据追溯,问题出在api.py中的serializer.is_valid行中,我不知道是什么原因造成的。

api.py

from rest_framework import generics, permissions
from rest_framework.response import Response
from knox.models import AuthToken
from .serializers import UserSerializer, RegisterSerializer


class RegisterAPI(generics.GenericAPIView):
  serializer_class = RegisterSerializer

  def post(self, request, *args, **kwargs):
    serializer = self.get_serializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    user = serializer.save()
    return Response({
      "user": UserSerializer(user, context=self.get_serializer_context()).data,
      "token": AuthToken.objects.create(user)
    })

serializers.py

from rest_framework import serializers
from django.contrib.auth.models import User
from django.contrib.auth import authenticate

# User Serializer
class UserSerializer(serializers.ModelSerializer):
  class Meta:
    model = User
    fields = ('id', 'username', 'email')

# Register Serializer
class RegisterSerializer(serializers.ModelSerializer):
  class Meta:
    model = User
    fields = ('id', 'username', 'email', 'password')
    extra_kwargs = {'password': {'write_only': True}}

  def create(self, validated_data):
    user = User.objects.create_user(validated_data['username'], validated_data['email'], validated_data['password'])
    return user

追踪

File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/tspc/workunteerpro/workunteer/accounts/api.py", line 12, in post
    serializer.is_valid(raise_exception=True)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/serializers.py", line 236, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/serializers.py", line 434, in run_validation
    value = self.to_internal_value(data)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/serializers.py", line 485, in to_internal_value
    fields = self._writable_fields
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/serializers.py", line 370, in _writable_fields
    field for field in self.fields.values() if not field.read_only
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/serializers.py", line 363, in fields
    for key, value in self.get_fields().items():
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/serializers.py", line 1024, in get_fields
    info = model_meta.get_field_info(model)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/utils/model_meta.py", line 39, in get_field_info
    forward_relations = _get_forward_relationships(opts)
  File "/Users/tspc/.local/share/virtualenvs/workunteerpro-aGYRWm4E/lib/python3.7/site-packages/rest_framework/utils/model_meta.py", line 96, in _get_forward_relationships
    not field.remote_field.through._meta.auto_created
AttributeError: 'NoneType' object has no attribute '_meta'

0 个答案:

没有答案