我对为什么在品味资源中需要save_m2m的理解还不清楚。在POST中,如果我只发布与创建一个模型相关的数据,而不发送与m2m对象相关的任何内容,我是否仍然需要执行save_m2m。为什么需要它?如果我覆盖save_m2m不做任何事情,会发生什么?它似乎工作得很好,我的资源也被创建了,我不确定这可能会导致什么隐藏的影响。你能评论一下吗。
发布于 2012-09-05 22:23:27
如果你没有任何标记为is_m2m=True的字段,那么这个方法实际上不会做任何事情。来自save_m2m中的tastypie文档字符串:
"""
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快一个循环(一个小小的加速!;)。
https://stackoverflow.com/questions/10033997
复制相似问题