[Answer]-How to disable create a resource if it exists?

1👍

You can try something like this:

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   

Or you can also use validation.

👤msc

Leave a comment