python - tastypie 中的外键

标签 python django rest tastypie

所以,我开始使用 TastyPie Django 插件为我的项目制作 REST api。我正在按照我的项目入门指南进行操作,但是当我进入 this point 时,当我应该放一个外键时,它开始给我一些错误。

当我做一个简单的 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

最佳答案

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

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

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

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

关于python - tastypie 中的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5756205/

相关文章:

javascript - 从 django 上的 js 获取

html - 我的 CSS 文件未在 Django 中加载,但 Bootstrap 文件加载正常

php - 在数据库或文件系统中缓存 API 响应

php - 这种基于服务器的数据存储方法是否走在正确的轨道上?

python - 使用文件存储 optparse 参数

python - 求有约束的方程的根

python - 403 禁止 : Nginx not serving statics in Django Admin panel

python - 使用 django 编码字符串作为 django 信号的发送者

asp.net - 如何在 REST Web 服务中处理使用 100 Continue?

python - 按前 N 个字符对字符串进行排序