0

我可以创建一个使用 post 方法,但是,如果文本已经在数据库MessageResource中,我想返回类似“消息存在”的内容。MessageResource如何实施?我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"
4

1 回答 1

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   

或者您也可以使用验证

于 2012-11-13T15:25:47.773 回答