编程语言
首页 > 编程语言> > python – tastypie中的外键

python – tastypie中的外键

作者:互联网

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

当我做一个简单的获取时,一个是这个:

"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,rest,django,tastypie
来源: https://codeday.me/bug/20190626/1296337.html