ListCreateAPIView 对象没有属性“_default_manager”

ListCreateAPIView object has no attribute '_default_manager'

我有这些序列化程序:

# Tickets
class TicketSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ticket
        depth = 2

class TicketWriteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ticket

和这个观点:

class Tickets(generics.ListCreateAPIView):
  model = Ticket
  serializer_class = TicketSerializer

  def create(self, request, *args, **kwargs):
    serializer = TicketWriteSerializer(data=request.DATA, files=request.FILES)
    if serializer.is_valid():
      self.pre_save(serializer.object)
      self.object = serializer.save(force_insert=True)
      self.post_save(self.object, created=True)
      headers = self.get_success_headers(serializer.data)
      serializer = TicketSerializer(serializer.object)
      return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

在我的 urls 文件中,我这样添加它:

  url(r'^tickets/', Tickets.as_view(model=Tickets), name='tickets')

对于这个模型:

class Ticket(models.Model):
    customer = models.ForeignKey('Customer', null=True, blank=True)
    act = models.ForeignKey('Act')
    row = models.PositiveIntegerField()
    seat = models.PositiveIntegerField()
    # Price for a seat can go up to 9999.99
    price = models.DecimalField(max_digits=6, decimal_places=2)
    status = models.CharField(max_length=255)   

现在,每当我调用 /tickets/ 时,我都会得到:

AttributeError at /tickets/

type object 'Tickets' has no attribute '_default_manager'

我想知道这是怎么回事。 我想要的是两个序列化程序,这样我就可以像使用 ForeignKeys 一样发送数据 "flat",但会使用深度 = 2 的所有数据检索它们。

编辑: 回溯:

Environment:

Request Method: GET Request URL: http://localhost:8000/tickets/

Django Version: 1.7.1 Python Version: 3.4.2 Installed Applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'server', 'rest_framework', 'rest_framework.authtoken') Installed Middleware: ('django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback: File "/usr/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/usr/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view 57. return view_func(*args, **kwargs) File "/usr/lib/python3.4/site-packages/django/views/generic/base.py" in view 69. return self.dispatch(request, *args, **kwargs) File "/usr/lib/python3.4/site-packages/rest_framework/views.py" in dispatch 403. response = self.handle_exception(exc) File "/usr/lib/python3.4/site-packages/rest_framework/views.py" in dispatch 400. response = handler(request, *args, **kwargs) File "/usr/lib/python3.4/site-packages/rest_framework/generics.py" in get 493. return self.list(request, *args, **kwargs) File "/usr/lib/python3.4/site-packages/rest_framework/mixins.py" in list 76. self.object_list = self.filter_queryset(self.get_queryset()) File "/usr/lib/python3.4/site-packages/rest_framework/generics.py" in get_queryset 296. return self.model._default_manager.all()

Exception Type: AttributeError at /tickets/ Exception Value: type object 'Tickets' has no attribute '_default_manager'

您设置了 model=Tickets,但 Tickets 是您的视图,而不是您的模型。

url(r'^tickets/', Tickets.as_view(model=Tickets), name='tickets')

改用这个

url(r'^tickets/', Tickets.as_view(model=Ticket), name='tickets')