python - 在 tastypie 中发布 url

标签 python django tastypie

我是 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 网址是,

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

对上述链接的发布请求正在将该数据保存到数据库中。但我怀疑我是否可以使用单独的链接来发布或覆盖当前网址,以便我可以将发布请求发送到该链接。喜欢

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

最佳答案

您可以使用 prependurls 为帖子创建单独的 url,有用 link

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'),
        ]

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

相关文章:

django - 找不到 'filer_folder_changelist' 的反向

python - 安装 django 1.9 后,Django 应用程序未显示在管理中

django - 使用 easy-thumbnails 为 Tastypie API 生成缩略图

python - 用 0 填充缺失的日期(日)值

python matplotlib x轴标签全部显示但1

python - 如何解决pygame中这些圆圈的碰撞?

python - 查找数据帧行中的前 n 个值 (Python)

python - 如何更改 Django 电子邮件中的发件人地址?

django tastypie 仅获取特定对象的特定字段

python - 导入错误: No module named tastypie. API