我可以使用post方法创建MessageResource
,但是,如果MessageResource
文本已经存在于数据库中,我想返回类似“message exists”的内容。怎么实现呢?我的MessageResource
是
class MessageResource(ModelResource):
class Meta:
queryset = Message.objects.all()
resource_name = "message"
always_return_data = True
authentication = Authentication()
authorization = Authorization()
filtering = {
'body': ALL
}
def determine_format(self, request):
return "application/json"
答案 0 :(得分:1)
您可以尝试这样的事情:
from tastypie.exceptions import BadRequest
class MessageResource(ModelResource):
class Meta:
queryset = Message.objects.all()
resource_name = "message"
always_return_data = True
authentication = Authentication()
authorization = Authorization()
filtering = {
'body': ALL
}
def determine_format(self, request):
return "application/json"
def hydrate(self, bundle):
if not bundle.obj.pk and len(Message.objects.filter(text=bundle.obj.text)) > 0:
raise BadRequest('Message exists')
return bundle
或者您也可以使用validation。