django-tastypie水合物m2m与通过表

时间:2013-06-29 09:15:40

标签: tastypie

我正在尝试保存与ToMany相关的资源以及创建时的一些复杂数据处理,并希望找出将我的巨型,kludgy obj_create重构为在范式内有意义的最佳方法。 tastypie。

目前,我的obj_create看起来像这样:

@transaction.commit_manually
def obj_create(self, bundle, **kwargs):
    sid = transaction.savepoint()
    try:
        data = self.deserialize(bundle.request, bundle.request.body)
        related_data = data.pop('related')
        ## get_or_create the related objects by processing the data passed
        related_objs = self.create_related_objs(related_data)

        ## Create the object the above are related to
        obj = self._meta.object_class.objects.create(**data)  # this has been cleaned of non-object keys
        for (order, related) in enumerate(related_objs):
            ObjRelation.objects.create(obj=obj, related=related, order=order)
    except:
        transaction.savepoint_rollback(sid)
        raise Badrequest("you fail it")
    bundle.obj = obj
    bundle.data = data
    transaction.savepoint_commit(sid)
    return bundle

所以这是我正在做的相当简化的版本。我相信将相关的保存移动到fields.ToManyField的子类会更好,但我看不出需要采用哪种方法来覆盖。或者将它放入hydrate_related

会更好吗?

0 个答案:

没有答案