0

using 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 に答える