本文整理汇总了Python中modelcluster.fields.ParentalManyToManyField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.ParentalManyToManyField方法的具体用法?Python fields.ParentalManyToManyField怎么用?Python fields.ParentalManyToManyField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类modelcluster.fields
的用法示例。
在下文中一共展示了fields.ParentalManyToManyField方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_all_child_m2m_relations
# 需要导入模块: from modelcluster import fields [as 别名]
# 或者: from modelcluster.fields import ParentalManyToManyField [as 别名]
def get_all_child_m2m_relations(model):
"""
Return a list of ParentalManyToManyFields on the given model,
including ones attached to ancestors of the model
"""
return [
field for field in model._meta.get_fields()
if isinstance(field, ParentalManyToManyField)
]
示例2: select_on_queryset
# 需要导入模块: from modelcluster import fields [as 别名]
# 或者: from modelcluster.fields import ParentalManyToManyField [as 别名]
def select_on_queryset(self, queryset):
"""
This method runs either prefetch_related or select_related on the queryset
to improve indexing speed of the relation.
It decides which method to call based on the number of related objects:
- single (eg ForeignKey, OneToOne), it runs select_related
- multiple (eg ManyToMany, reverse ForeignKey) it runs prefetch_related
"""
try:
field = self.get_field(queryset.model)
except FieldDoesNotExist:
return queryset
if isinstance(field, RelatedField) and not isinstance(field, ParentalManyToManyField):
if field.many_to_one or field.one_to_one:
queryset = queryset.select_related(self.field_name)
elif field.one_to_many or field.many_to_many:
queryset = queryset.prefetch_related(self.field_name)
elif isinstance(field, ForeignObjectRel):
# Reverse relation
if isinstance(field, OneToOneRel):
# select_related for reverse OneToOneField
queryset = queryset.select_related(self.field_name)
else:
# prefetch_related for anything else (reverse ForeignKey/ManyToManyField)
queryset = queryset.prefetch_related(self.field_name)
return queryset