本文介绍了不能发布到DRF modelviewset - request.data立即清空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在django模型的前面设置一个休息终点,我已经完成了绝对最小化的感觉,但是我不能从可浏览的API发布。
I've done what feels like the absolute minimum in setting up a rest endpoint in front of a django model, but I cannot post to it from the browseable API.
在 djangorestframework == 3.3.0
,所有帖子为 / api / transactions /
被拒绝。
On djangorestframework==3.3.0
, all posts to /api/transactions/
are rejected.
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."
]
}
我错过了什么?
推荐答案
我可以通过身份验证功能发布到终端。显然,它与有关DRF
I can post to the endpoint with authentication disabled. Apparently, it is related to this bug in DRF
这篇关于不能发布到DRF modelviewset - request.data立即清空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!