无法发布到DRF modelviewset - request.data立即清空

时间:2015-11-05 22:57:40

标签: python django django-rest-framework

我在django模型前设置了一个休息端点,感觉就像绝对最小,但是我无法从可浏览的API发布它。

djangorestframework==3.3.0上,/api/transactions/的所有帖子都被拒绝。

models.py

class TransactionQuerySet(models.QuerySet):
    ...

class Transaction(models.Model):
    objects = TransactionQuerySet.as_manager()

    id = models.UUIDField(primary_key=True,
                          editable=False,
                          default=uuid4,
                          unique=True)

    description = models.CharField(max_length=120)
    timestamp = models.DateTimeField(default=get_timestamp,
                                     editable=False)
    amount = models.DecimalField(max_digits=8, decimal_places=2)

    def __unicode__(self):
        return '{0} ({1})'.format(self.description,
                                  to_decimal(self.amount))

    class Meta:
        ordering = ['-timestamp']

serializers.py

class TransactionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Transaction
        exclude = ()

views.py

class TransactionViewSet(viewsets.ModelViewSet):
    queryset = Transaction.objects.all()
    serializer_class = TransactionSerializer

tracking.urls

from rest_framework.routers import DefaultRouter
from django.conf.urls import url

import views

router = DefaultRouter()
router.register(r'transactions', views.TransactionViewSet)
urlpatterns = router.urls

urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^api/api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api/', include('tracking.urls')),
]

提交表单获得此回复

HTTP 400 Bad Request
Content-Type: application/json
Vary: Accept
Allow: GET, POST, HEAD, OPTIONS

{
    "amount": [
        "This field is required."
    ],
    "description": [
        "This field is required."
    ]
}

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我可以在禁用身份验证的情况下发布到端点。显然,它与DRF中的this bug

有关