Improve instance lookup for metadata layer

- Existing call to get_object() could take > 20 seconds in some cases
- Not really sure why, some issue with the DRF library
- Was probably parsing the entire queryset rather than doing a PK lookup
- Instead, directly use the provided pk to get the model
This commit is contained in:
Oliver 2021-09-08 12:18:07 +10:00
parent a70f4c86eb
commit 919b39515f

View File

@ -98,10 +98,13 @@ class InvenTreeMetadata(SimpleMetadata):
serializer_info = super().get_serializer_info(serializer) serializer_info = super().get_serializer_info(serializer)
try: model_class = None
ModelClass = serializer.Meta.model
try:
model_class = serializer.Meta.model
model_fields = model_meta.get_field_info(model_class)
model_fields = model_meta.get_field_info(ModelClass)
# Iterate through simple fields # Iterate through simple fields
for name, field in model_fields.fields.items(): for name, field in model_fields.fields.items():
@ -146,10 +149,22 @@ class InvenTreeMetadata(SimpleMetadata):
if hasattr(serializer, 'instance'): if hasattr(serializer, 'instance'):
instance = serializer.instance instance = serializer.instance
if instance is None: if instance is None and model_class is not None:
# Attempt to find the instance based on kwargs lookup
kwargs = getattr(self.view, 'kwargs', None)
if kwargs:
pk = None
for field in ['pk', 'id', 'PK', 'ID']:
if field in kwargs:
pk = kwargs[field]
break
if pk is not None:
try: try:
instance = self.view.get_object() instance = model_class.objects.get(pk=pk)
except: except (ValueError, model_class.DoesNotExist):
pass pass
if instance is not None: if instance is not None: