不与模型绑定的Django Piston处理程序的ImportError

时间:2013-02-05 06:02:59

标签: django importerror django-piston

我创建了一个自定义处理程序(CustomHandler),它与ORM中的模型无关,我认为它已正确绑定,但我得到的是 ImportError: cannot import CustomHandler < / strong>尝试将其导入我的resources.py时。这是我的设置:

custom_handlers.py

from piston.handler import BaseHandler

class CustomHandler(BaseHandler):
    allowed_methods = ('GET',)

    def read(self, request):
        return 'test'

resources.py

from piston.resource import Resource
from piston.utils import rc
import simplejson as json
from api.authentication import DjangoAuthentication
from api.handlers import CustomHandler # ERROR THROWN HERE

auth = DjangoAuthentication(realm='...')

class JSONResource(Resource):
    def determine_emitter(self, request, *args, **kwargs):
        """
        Default to the json emitter.
        """
        try:
            return kwargs['emitter_format']
        except KeyError:
            pass
        if 'format' in request.GET:
            return request.GET.get('format')
        return 'json'

    def form_validation_response(self, e):
        """
        Turns the error object into a serializable construct.
        """
        resp = rc.BAD_REQUEST
        json_errors = json.dumps(
            dict(
                (k, map(unicode, v))
                for (k, v) in e.form.errors.iteritems()
            )
        )
        resp.write(json_errors)
        return resp


custom_handler = JSONResource(CustomHandler, authentication=auth)

urls.py

from django.conf.urls.defaults import patterns, url

from api.resources import custom_handler

urlpatterns = patterns('',
    url(r'^things/$', custom_handler),
)

更新:我尝试将py手动编译为pyc,但没有运气。我也在Piston's docs中看到了这个:

  

当你创建一个绑定到模型的处理程序时,Piston会   自动注册(通过元类。)

但我在文档中找不到任何关于创建与模型无关的处理程序的内容,特别是如何注册

1 个答案:

答案 0 :(得分:0)

必须将from api.handlers.custom_handlers import CustomHandler添加到api/handlers/__init__.py