在tastypi中发布URL

2024-10-03 06:27:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我对django的tastypie很陌生。我有一个组织模式。在api.py公司你知道吗

class OrganisationResource(ModelResource):
    create_user = fields.ForeignKey(PersonResource, 'create_user', null=True, full=True)
    update_user = fields.ForeignKey(PersonResource, 'update_user', null=True, full=True)
    location = fields.ForeignKey(LocationResource, 'location', null=True, full=True)
    class Meta:
        allowed_methods = ['post','get','delete','patch','put']
        queryset = Organization.objects.all()
        resource_name = 'organisation'
        authorization = Authorization()
        authentication = Authentication()
        always_return_data = True

api url是

http://127.0.0.1:8000/api/v1/organisation/

对上述链接的post请求正在将该数据保存到数据库中。但我的疑问是,我是否可以使用一个单独的链接发布或覆盖当前的网址,以便我可以发送后的要求,该链接。就像

http://127.0.0.1:8000/api/v1/organisation/create

Tags: apitruefields链接createupdatelocationpost
1条回答
网友
1楼 · 发布于 2024-10-03 06:27:18

您可以使用prependurls有用的link为文章创建单独的url

class OrganisationCreateResource(ModelResource):
    create_user = fields.ForeignKey(PersonResource, 'create_user', null=True, full=True)
    update_user = fields.ForeignKey(PersonResource, 'update_user', null=True, full=True)
    location = fields.ForeignKey(LocationResource, 'location', null=True, full=True)
    class Meta:
        allowed_methods = ['post']
        queryset = Organization.objects.all()
        detail_uri_name = 'create'
        resource_name = 'organisation'
        authorization = Authorization()
        authentication = Authentication()
        always_return_data = True
   def prepend_urls(self):
        return [
            url(r'^(?P<resource_name>%s)/create/' % self._meta.resource_name, self.wrap_view('dispatch_detail'), name='api_dispatch_detail'),
        ]

相关问题 更多 >