django-tastypie 尝试根据 POST 请求创建相关对象

发布于 2025-01-04 12:50:01 字数 1222 浏览 0 评论 0原文

当我尝试通过我的项目 API 创建带有外键(本例中为 2)的对象时,tastypie 也尝试创建相关对象(此处的订单和参与者):

class ParticipationResource(ModelResource):
    order = fields.ForeignKey(Order, 'order', full=True,)
    participant = fields.ForeignKey(UserProfile, 'participant', full=True)

    class Meta:
        authorization = Authorization()
        queryset = Participation.objects.all()
        resource_name = 'participation'
        fields = ['id', 'order', 'participant', 'products', 'created_at', 'modified_at']
        filtering = {
            'participant': ALL
        }
        detail_allowed_methods = ['get', 'post', 'put', 'delete',]
        always_return_data = True

发布的数据:

 {
     "order": {
         "id":"1", 
         "resource_uri":"/api/v1/order/1/"
         ...
      },
     "participant":{
         "id":"1",
         "resource_uri":"/api/v1/participant/1/"
         ...
      },
     "products":[]
  }

错误消息(network_id 是上的外键)用户配置文件模型):

"error_message": "app_user_profile.network_id may not be NULL",

正如您所看到的,我在 POST 请求中发送了完整的对象,我尝试仅使用 resources_uri 并且工作正常,问题是我需要完整的对象进行客户端渲染(我是使用 Backbone 和模型自动渲染)。那我该怎么办呢?有没有办法告诉 tastypie 不要创建相关对象?

When I try to create an object with a foreignkey (2 in this case) through my project API, tastypie try to create the related objects as well (order & participant here):

class ParticipationResource(ModelResource):
    order = fields.ForeignKey(Order, 'order', full=True,)
    participant = fields.ForeignKey(UserProfile, 'participant', full=True)

    class Meta:
        authorization = Authorization()
        queryset = Participation.objects.all()
        resource_name = 'participation'
        fields = ['id', 'order', 'participant', 'products', 'created_at', 'modified_at']
        filtering = {
            'participant': ALL
        }
        detail_allowed_methods = ['get', 'post', 'put', 'delete',]
        always_return_data = True

Data posted:

 {
     "order": {
         "id":"1", 
         "resource_uri":"/api/v1/order/1/"
         ...
      },
     "participant":{
         "id":"1",
         "resource_uri":"/api/v1/participant/1/"
         ...
      },
     "products":[]
  }

Error message (network_id is a foreign key on the user profile model) :

"error_message": "app_user_profile.network_id may not be NULL",

As you can see I'm sending the full objects in my POST request, I tried with only the resource_uri and it worked fine, the problem is that I need the full objects for client side rendering (I'm using Backbone and the model is automatically rendered). So how can I do ? Is there a way to tell tastypie to not create the related objects ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2025-01-11 12:50:01

当您将 TastyPie 配置为 full=True 时,它​​会以两种方式工作 - 它返回完整嵌套对象,但也接受完整嵌套对象。

您需要做的是将 Backbone 配置为不发送嵌套对象的完整 JSON,而仅发送 resource_uri。

有几种方法可以做到这一点 - 一种方法是仅使用 Backbone-Relational 它负责解析和创建嵌套模型 - 因此您不必执行自定义 parse()。

另一种方法是使用 Backbone-Tastypie (由同一作者),它是 Backbone-Relational 代码的子集使用 TastyPie 更轻松。

但是,如果您想坚持使用现有的内容,则需要编写一个自定义 toJSON 函数,该函数仅返回 resource_uri 而不是嵌套对象。

这使主干模型保持完整,但是当它们序列化发送到服务器时,仅使用资源 uri:

 {
     "order": "/api/v1/order/1/",
     "participant":"/api/v1/participant/1/",
     "products":[]
  }

When you configure TastyPie for full=True, it works both ways - it returns full nested objects, but also accepts full nested objects.

What you need to do is configure Backbone to not SEND the full JSON of the nested objects, but just the resource_uri.

There's a couple ways to do this - one way is to just use Backbone-Relational which takes care of the parsing and creation of nested models - so you don't have to do a custom parse().

Another is to use Backbone-Tastypie (by the same author) which is a subset of the Backbone-Relational code that makes it easier to work with TastyPie.

But, if you want to stick with what you have, you need to write a custom toJSON function that simply returns the resource_uri instead of a nested object.

This keeps the Backbone Models intact, but when their serialized to be sent to the server, only the resource uri is used:

 {
     "order": "/api/v1/order/1/",
     "participant":"/api/v1/participant/1/",
     "products":[]
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文