mirror of
https://github.com/inventree/InvenTree.git
synced 2025-12-16 17:28:11 +00:00
Add generic relationship to allow reverse lookups
This commit is contained in:
@@ -6,6 +6,7 @@ from string import Formatter
|
|||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.contrib.contenttypes.fields import GenericRelation
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import QuerySet
|
from django.db.models import QuerySet
|
||||||
@@ -500,6 +501,8 @@ class InvenTreeParameterMixin(InvenTreePermissionCheckMixin):
|
|||||||
and provides the following methods:
|
and provides the following methods:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
parameters = GenericRelation('common.Parameter', related_query_name='+')
|
||||||
|
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
"""Handle the deletion of a model instance.
|
"""Handle the deletion of a model instance.
|
||||||
|
|
||||||
@@ -508,18 +511,6 @@ class InvenTreeParameterMixin(InvenTreePermissionCheckMixin):
|
|||||||
self.parameters.all().delete()
|
self.parameters.all().delete()
|
||||||
super().delete(*args, **kwargs)
|
super().delete(*args, **kwargs)
|
||||||
|
|
||||||
@property
|
|
||||||
def parameters(self) -> QuerySet:
|
|
||||||
"""Return a queryset containing all parameters for this model."""
|
|
||||||
return self.parameters_for_model().filter(model_id=self.pk)
|
|
||||||
|
|
||||||
def parameters_for_model(self) -> QuerySet:
|
|
||||||
"""Return a QuerySet containing all parameters for this model class."""
|
|
||||||
from common.models import Parameter
|
|
||||||
|
|
||||||
model_type = self.__class__.__name__.lower()
|
|
||||||
return Parameter.objects.filter(model_type=model_type)
|
|
||||||
|
|
||||||
|
|
||||||
class InvenTreeAttachmentMixin(InvenTreePermissionCheckMixin):
|
class InvenTreeAttachmentMixin(InvenTreePermissionCheckMixin):
|
||||||
"""Provides an abstracted class for managing file attachments.
|
"""Provides an abstracted class for managing file attachments.
|
||||||
|
|||||||
Reference in New Issue
Block a user