【发布时间】:2014-06-25 23:37:25
【问题描述】:
我正在开发一个 REST API,它从一些无法 PATCH 或其他任何东西的脑死软件接收 POST 请求。 POST 用于更新数据库中已经存在的模型对象。
具体来说,我正在为具有相关字段(一个 SlugRelatedField,因为 POSTer 知道“名称”属性但不知道“pk”)的对象发布数据。但是,如果 POSTer 发送的数据在 SlugRelatedField 上没有返回任何“名称”(例如,相关对象不存在),我需要返回 404。我已经用调试器完成了这个,但似乎 DRF 使用了一些 Django 信号魔法来做到这一点 The Way DRF Does It™,即返回 400 BAD REQUEST。我不知道如何修改这个 - 只有当它是上述条件而不是真正的 400-worthy POST - 到 404。
顺便说一句,在我看来,pre_save() 在失败的测试执行期间没有执行。
这是序列化程序:
class CharacterizationSerializer(serializers.ModelSerializer):
"""
Work-in-progress for django-rest-framework use. This handles (de)serialization
of data into a Characterization object and vice versa.
See: http://www.django-rest-framework.org/tutorial/1-serialization
"""
creator = serializers.Field(source='owner.user.username')
sample = serializers.SlugRelatedField(slug_field='name',
required=True,
many=False,
read_only=False)
class Meta:
model = Characterization
# leaving 'request' out because it's been decided to deprecate it. (...maybe?)
fields = ('sample', 'date', 'creator', 'comments', 'star_volume', 'solvent_volume',
'solution_center', 'solution_var', 'solution_minimum', 'solution_min_stddev',
'solution_test_len',)
这是pre_save 没有在给定测试中运行的视图(但在其他一些测试中运行):
class CharacterizationList(generics.ListCreateAPIView):
queryset = Characterization.objects.all()
serializer_class = CharacterizationSerializer
permission_classes = (AnonPostAllowed,) # @todo XXX hack for braindead POSTer
def pre_save(self, obj):
# user isn't sent as part of the serialized representation,
# but is instead a property of the incoming request.
if not self.request.user.is_authenticated():
obj.owner = get_dummy_proxyuser() # this is done for CharacterizationList so unauthed users can POST. @todo XXX hack
else:
obj.owner = ProxyUser.objects.get(pk=self.request.user.pk)
# here, we're fed a string sample name, but we need to look up
# the actual sample model.
# @TODO: Are we failing properly if it doesn't exist? Should
# throw 404, not 400 or 5xx.
# except, this code doesn't seem to be run directly when debugging.
# a 400 is thrown; DRF must be bombing out before pre_save?
obj.sample = Sample.objects.get(name=self.request.DATA['sample'])
为了更好的衡量,这是失败的测试:
def test_bad_post_single_missing_sample(self):
url = reverse(self._POST_ONE_VIEW_NAME)
my_sample_postdict = self.dummy_plqy_postdict.copy()
my_sample_postdict["sample"] = "I_DONT_EXIST_LUL"
response = self.rest_client.post(url, my_sample_postdict)
self.assertTrue(response.status_code == 404,
"Expected 404 status code, got %d (%s). Content: %s" % (response.status_code, response.reason_phrase, response.content))
如果我在 self.rest_client.post() 调用处设置断点,则响应中已经包含 400。
【问题讨论】:
标签: python django django-rest-framework django-signals