tastypie中的外键
我开始使用TastyPie这个插件来为我的Django项目制作一个REST API。我在按照入门指南进行操作,但当我到达这个步骤时,准备添加一个外键时,出现了一些错误。
最大的错误是,当我进行简单的获取操作时,出现了这个问题:
"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
2 个回答
1
看起来你的 urlpatterns
可能被覆盖了。
urlpatterns += api.urls;
这样加上 +=
有用吗?直接给 urlpatterns
赋值的话,可能会意外地把之前的内容给覆盖掉。
3
我问题的最终解决方案是结合了@manji和@dlrust的回答:
“把urlpatterns
的值改成urlpatterns = patterns('', (r'^api/', include(api.urls)),)
。”
然后,“在你的Meta里为这个资源定义一个授权。”
希望这对其他人也有用,就像对我一样 :)