From f3ed05a09e4f5ccf1b65577a0189c80b85c34be6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 28 Jun 2021 20:13:18 +1000 Subject: [PATCH] Automatically associate ''filters' with relations --- InvenTree/InvenTree/metadata.py | 17 ++++++++++ .../company/templates/company/index.html | 33 ++++++++++++++++--- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/InvenTree/InvenTree/metadata.py b/InvenTree/InvenTree/metadata.py index 85815d2558..89b5b14493 100644 --- a/InvenTree/InvenTree/metadata.py +++ b/InvenTree/InvenTree/metadata.py @@ -97,6 +97,7 @@ class InvenTreeMetadata(SimpleMetadata): model_fields = model_meta.get_field_info(ModelClass) + # Iterate through simple fields for name, field in model_fields.fields.items(): if field.has_default() and name in field_info.keys(): @@ -110,9 +111,25 @@ class InvenTreeMetadata(SimpleMetadata): continue field_info[name]['default'] = default + + # Iterate through relations + for name, relation in model_fields.relations.items(): + + if relation.reverse: + print("skipping reverse relation -", name) + continue + + print('filters:', name, relation.model_field.get_limit_choices_to()) + + continue + # Extract and provide the "limit_choices_to" filters + field_info[name]['filters'] = relation.model_field.get_limit_choices_to() + except AttributeError: pass + print(field_info.keys()) + return field_info def get_field_info(self, field): diff --git a/InvenTree/company/templates/company/index.html b/InvenTree/company/templates/company/index.html index 28129b1f36..f02ae1e6c8 100644 --- a/InvenTree/company/templates/company/index.html +++ b/InvenTree/company/templates/company/index.html @@ -40,12 +40,34 @@ $('#new-company-2').click(function() { constructForm( - '{% url "api-company-list" %}', + '{% url "api-build-list" %}', { method: 'POST', - fields: [ - 'name', - 'description', + title: '{% trans "Edit Part Details" %}', + fields: { + name: { + onEdit: function() { + console.log('Edited name field'); + } + }, + description: {}, + category: { + filters: { + parent: 1, + }, + secondary: { + label: '{% trans "New Category" %}', + }, + }, + active: { + onEdit: function() { + console.log('edited active field'); + } + }, + purchaseable: {}, + salable: {}, + component: {}, + /* 'website', 'address', 'phone', @@ -54,7 +76,8 @@ 'is_supplier', 'is_manufacturer', 'is_customer', - ] + */ + } } ); });