我试图ToMany
在创建时通过 a 和一些复杂的数据处理来保存相关的资源,并想找出将我的巨型 kludgy obj_create 重构为在美味派范式中有意义的东西的最佳方法。
目前,我的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
?