Automatically associate ''filters' with relations

This commit is contained in:
Oliver 2021-06-28 20:13:18 +10:00
parent e585079779
commit f3ed05a09e
2 changed files with 45 additions and 5 deletions

View File

@ -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):

View File

@ -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',
]
*/
}
}
);
});