2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-13 10:35:40 +00:00

Create a custom Manager class for the Part model

- Always perform prefetch_related calls
This commit is contained in:
Oliver
2021-07-20 21:26:51 +10:00
parent d9673244d5
commit 84fc2785d6
2 changed files with 19 additions and 19 deletions

View File

@ -284,6 +284,23 @@ def match_part_names(match, threshold=80, reverse=True, compare_length=False):
return matches
class PartManager(models.Manager):
"""
Defines a custom object manager for the Part model.
The main purpose of this manager is to reduce the number of database hits,
as the Part model has a large number of ForeignKey fields!
"""
def get_queryset(self):
return super().get_queryset().prefetch_related(
'category',
'stock_items',
'builds',
)
@cleanup.ignore
class Part(MPTTModel):
""" The Part object represents an abstract part, the 'concept' of an actual entity.
@ -321,6 +338,8 @@ class Part(MPTTModel):
responsible: User who is responsible for this part (optional)
"""
objects = PartManager()
class Meta:
verbose_name = _("Part")
verbose_name_plural = _("Parts")