当前位置: 首页>>代码示例>>Python>>正文


Python NestedObjects.sort方法代码示例

本文整理汇总了Python中django.contrib.admin.util.NestedObjects.sort方法的典型用法代码示例。如果您正苦于以下问题:Python NestedObjects.sort方法的具体用法?Python NestedObjects.sort怎么用?Python NestedObjects.sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.contrib.admin.util.NestedObjects的用法示例。


在下文中一共展示了NestedObjects.sort方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: deepcopy

# 需要导入模块: from django.contrib.admin.util import NestedObjects [as 别名]
# 或者: from django.contrib.admin.util.NestedObjects import sort [as 别名]
 def deepcopy(self, newname, value=None, field=None, duplicate_order=None):
     """
     Duplicate all related objects of obj setting
     field to value. If one of the duplicate
     objects has an FK to another duplicate object
     update that as well. Return the duplicate copy
     of obj.
     duplicate_order is a list of models which specify how
     the duplicate objects are saved. For complex objects
     this can matter. Check to save if objects are being
     saved correctly and if not just pass in related objects
     in the order that they should be saved.
     """
     obj = self
     collector = NestedObjects(using='default')
     collector.collect([obj])
     collector.sort()
     related_models = collector.data.keys()
     data_snapshot =  {}
     for key in collector.data.keys():
         data_snapshot.update({ key: dict(zip([item.pk for item in collector.data[key]], [item for item in collector.data[key]])) })
     root_obj = None
 
     # Sometimes it's good enough just to save in reverse deletion order.
     if duplicate_order is None:
         duplicate_order = reversed(related_models)
 
     for model in duplicate_order:
         # Find all FKs on model that point to a related_model.
         fks = []
         for f in model._meta.fields:
             if isinstance(f, ForeignKey) and f.rel.to in related_models:
                 fks.append(f)
         # Replace each `sub_obj` with a duplicate.
         if model not in collector.data:
             continue
         sub_objects = collector.data[model]
         for obj in sub_objects:
             for fk in fks:
                 fk_value = getattr(obj, "%s_id" % fk.name)
                 # If this FK has been duplicated then point to the duplicate.
                 fk_rel_to = data_snapshot[fk.rel.to]
                 if fk_value in fk_rel_to:
                     dupe_obj = fk_rel_to[fk_value]
                     setattr(obj, fk.name, dupe_obj)
             # Duplicate the object and save it.
             obj.id = None
             if field is not None:
                 setattr(obj, field, value)
             obj.save()
             if root_obj is None:
                 root_obj = obj
     setattr(root_obj, 'name', newname)
     return root_obj
开发者ID:awaazde,项目名称:avaajotalo,代码行数:56,代码来源:models.py


注:本文中的django.contrib.admin.util.NestedObjects.sort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。