Include 'default' value in OPTIONS request for any fields with specified default values

This commit is contained in:
Oliver 2021-06-27 21:44:21 +10:00
parent 67f76c8bca
commit d80948369b
3 changed files with 61 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import logging
from rest_framework import serializers
from rest_framework.metadata import SimpleMetadata
from rest_framework.utils import model_meta
import users.models
@ -41,11 +42,11 @@ class InvenTreeMetadata(SimpleMetadata):
try:
# Extract the model name associated with the view
model = view.serializer_class.Meta.model
self.model = view.serializer_class.Meta.model
# Construct the 'table name' from the model
app_label = model._meta.app_label
tbl_label = model._meta.model_name
app_label = self.model._meta.app_label
tbl_label = self.model._meta.model_name
table = f"{app_label}_{tbl_label}"
@ -83,6 +84,37 @@ class InvenTreeMetadata(SimpleMetadata):
return metadata
def get_serializer_info(self, serializer):
"""
Override get_serializer_info so that we can add 'default' values
to any fields whose Meta.model specifies a default value
"""
field_info = super().get_serializer_info(serializer)
try:
ModelClass = serializer.Meta.model
model_fields = model_meta.get_field_info(ModelClass)
for name, field in model_fields.fields.items():
if field.has_default() and name in field_info.keys():
default = field.default
if callable(default):
try:
default = default()
except:
continue
field_info[name]['default'] = default
except AttributeError:
pass
return field_info
def get_field_info(self, field):
"""
Given an instance of a serializer field, return a dictionary

View File

@ -15,10 +15,13 @@
{% if pagetype == 'manufacturers' and roles.purchase_order.add or pagetype == 'suppliers' and roles.purchase_order.add or pagetype == 'customers' and roles.sales_order.add %}
<div id='button-toolbar'>
<button type='button' class="btn btn-success" id='new-company'>
<span class='fas fa-plus-circle'></span> {{ button_text }} (Server Side)
</button>
<button type='button' class="btn btn-success" id='new-company-2'>
<span class='fas fa-plus-circle'></span> {{ button_text }} (Client Side)
</button>
<div class='btn-group'>
<button type='button' class="btn btn-success" id='new-company'>
<span class='fas fa-plus-circle'></span> {{ button_text }}
</button>
</div>
</div>
{% endif %}
@ -35,6 +38,22 @@
});
});
$('#new-company-2').click(function() {
constructForm(
'{% url "api-company-list" %}',
{
method: 'POST',
fields: [
'name',
'description',
'is_supplier',
'is_manufacturer',
'is_customer',
]
}
);
});
loadCompanyTable("#company-table", "{% url 'api-company-list' %}",
{
pagetype: '{{ pagetype }}',

View File

@ -21,3 +21,7 @@ Create new release for the [inventree documentation](https://github.com/inventre
### Python Library Release
Create new release for the [inventree python library](https://github.com/inventree/inventree-python)
## App Release
Create new versioned release for the InvenTree mobile app.