【问题标题】:tastypie: save_m2m美味的:save_m2m
【发布时间】:2012-04-19 12:16:33
【问题描述】:

我对为什么在一个美味的资源中需要 save_m2m 的理解尚不清楚。在 POST 中,如果我只发布与创建一个模型相关的数据并且不发送与 m2m 对象相关的任何内容,我还需要执行 save_m2m.xml 吗?为什么需要它?如果我覆盖 save_m2m 什么都不做会发生什么?它似乎工作正常并且我的资源已创建,我不确定这可能会导致任何隐藏的影响。能不能给点意见。

【问题讨论】:

    标签: tastypie


    【解决方案1】:

    如果您没有任何标记为is_m2m=True 的字段,则该方法实际上不会执行任何操作。来自 save_m2m 中的 sweetpie 文档字符串:

    """
    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.
    """
    

    在tastepie 的资源save_m2m 方法中检查is_m2m 设置为True 的字段,如果没有找到它就什么也不做,所以如果你的资源类没有任何m2m 并且任何其他资源都不会继承从中,您可以覆盖 save_m2m 方法以不执行任何操作。

    你实际上会比美味派领先一个循环(一个小小的加速哇哦!;))。

    【讨论】:

      猜你喜欢
      • 2014-06-02
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2014-01-23
      • 1970-01-01
      • 2012-05-31
      相关资源
      最近更新 更多