首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >品味: save_m2m

品味: save_m2m
EN

Stack Overflow用户
提问于 2012-04-06 02:30:34
回答 1查看 889关注 0票数 1

我对为什么在品味资源中需要save_m2m的理解还不清楚。在POST中,如果我只发布与创建一个模型相关的数据,而不发送与m2m对象相关的任何内容,我是否仍然需要执行save_m2m。为什么需要它?如果我覆盖save_m2m不做任何事情,会发生什么?它似乎工作得很好,我的资源也被创建了,我不确定这可能会导致什么隐藏的影响。你能评论一下吗。

EN

回答 1

Stack Overflow用户

发布于 2012-09-05 22:23:27

如果你没有任何标记为is_m2m=True的字段,那么这个方法实际上不会做任何事情。来自save_m2m中的tastypie文档字符串:

代码语言:javascript
复制
"""
Handles the saving of related M2M data.

Due to the way Django works, the M2M data must be handled after the
main instance, which is why this isn't a part of the main ``save`` bits.

Currently slightly inefficient in that it will clear out the whole
relation and recreate the related data as needed.
"""

在tastypie的资源中,save_m2m方法检查is_m2m设置为True的字段,如果没有,它就不做任何事情,所以如果您的资源类没有任何m2m,并且任何其他资源不会从它继承,您可以覆盖save_m2m方法而不做任何事情。

你实际上会比tastypie快一个循环(一个小小的加速!;)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10033997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档