4

因此,我开始使用 Django 的TastyPie插件为我的项目制作 REST api。我在我的项目中遵循入门指南,但是当我进入这一点时,当我应该放置一个外键时,它开始给我一些错误。

当我做一个简单的get时,最重要的是:

"Reverse for 'api_dispatch_detail' with arguments '()' and keyword arguments '{'pk': 246, 'api_name': 'v1', 'resource_name': 'typep'}' not found."

resources.py 中的代码:

class TypeOfPlaceResource(ModelResource):

    class Meta:
        queryset = TypeOfPlace.objects.all()
        resource_name = 'typep'
        allowed_methods = ['get']

class POIResource(ModelResource):

    typep = ForeignKey(TypeOfPlaceResource, 'typep')

    class Meta:
        queryset = PointOfInterest.objects.all()
        resource_name = 'pois'
        filtering = {
            "code1": ALL,
            "code2": ALL,
        }

和模型:

class TypeOfPlace (models.Model):
    name = models.CharField(max_length=100, blank=True)
    code = models.CharField(max_length=20, unique=True)

    def __unicode__(self):
        return self.name

class PointOfInterest(GeoInformation):
    name = models.CharField(max_length=100,blank=True)
    code1 = models.CharField(max_length=4,null=True, unique=True)
    code2 = models.CharField(max_length=4,null=True, unique=True)
    typep = models.ForeignKey(TypeOfPlace)

    def __unicode__(self):
        return self.name

urls.py

api = Api(api_name='v1')
api.register(TypeOfPlaceResource(), canonical=True)
api.register(POIResource(), canonical=True)

urlpatterns = api.urls

那么,我做错了吗?还是错过了什么?任何帮助将非常感激 !:D

4

2 回答 2

3

我的问题的最终答案是@manji 和@dlrust 结合的答案:

“将urlpatterns值更改为urlpatterns = patterns('', (r'^api/', include(api.urls)),)

然后,“在您的 Meta 中为资源定义授权”。

希望它对其他人有用,就像对我一样:)

于 2012-01-01T05:46:40.527 回答
1

看起来你的urlpatterns可能会被覆盖。

urlpatterns += api.urls;

添加+=这样的工作吗?似乎通过直接分配给urlpatterns您可能会意外地破坏您拥有的任何旧分配。

于 2011-11-11T06:48:56.433 回答