from mptt.managers import TreeManager
from mptt.models import raise_if_unsaved, MPTTModel
class CustomManager(TreeManager):
@raise_if_unsaved
def get_children(self):
qs = super().get_children()
return qs[:3]
class Comment(MPTTModel):
# после всех полей
objects = TreeManager() # Менеджер по умолчанию
custom_objects = CustomManager()
# до class Meta:
# queryset внутри view
from django.db.models import Prefetch
comment_qs = Comment.custom_objects.all()
product_qs = Product.objects /
.prefetch_related(
Prefetch('comments', queryset=comment_qs)
)
zak = Zakaz.objects.get(id=self.zakaz_id)