Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions apps/dyn_dt/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ def model_dt(aPath):
fk_fields = get_model_fk_values(aModelClass)
db_filters = []
for f in db_fields:
if f not in fk_fields.keys():
db_filters.append( f )
# if f not in fk_fields.keys():
db_filters.append( f )
db_fields.extend(list(fk_fields.keys()))

choices_dict = {}
for column in aModelClass.__table__.columns:
Expand Down Expand Up @@ -252,16 +253,19 @@ def update(aPath, id):

if request.method == 'POST':
for attribute, value in request.form.items():
if hasattr(item, attribute) and getattr(item, attribute, value) is not None:
if hasattr(item, attribute) and (value != "" or getattr(item, attribute, value) is not None):
if attribute in fk_fields.keys():
old_value = value
table_name = None
for product in fk_fields[attribute]:
table_name = product.__class__.__tablename__
if table_name:
model_name = config.Config.DYNAMIC_DATATB[table_name]
value = name_to_class(model_name).query.filter_by(id=value).first()

setattr(item, attribute, value)
if value != getattr(item, attribute, old_value):
setattr(item, attribute, value)
elif value != getattr(item, attribute, value):
setattr(item, attribute, value)

db.session.commit()

Expand Down Expand Up @@ -352,3 +356,4 @@ def getenumattribute(value, arg):
@blueprint.app_template_filter('get')
def get(dict_data, key):
return dict_data.get(key, [])

8 changes: 5 additions & 3 deletions templates/dyn_dt/model.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ <h1 class="modal-title fs-5" id="editSalesLabel">Edit {{ link }}</h1>
<div class="form-group">
<label for="id_{{ key }}" class="form-label">{{ key }}</label>
<select class="form-control" name="{{ key }}" id="id_{{ key }}">
<option value="">Select {{ key }}</option>
{% for i in values %}
<option value="{{ i.id }}">{{ i }}</option>
<option {% if item|getattribute(key) == i %}selected="selected"{% endif %} value="{{ i.id }}">{{ i }}</option>
{% endfor %}
</select>
</div>
Expand Down Expand Up @@ -436,6 +437,7 @@ <h1 class="modal-title fs-5" id="addSalesLabel">Add {{ link }}</h1>
<div class="form-group">
<label for="id_{{ field }}" class="form-label">{{ field|title }}</label>
<select class="form-control" name="{{ field }}" id="id_{{ field }}">
<option selected="selected" value="">Select {{ field }}</option>
{% for i in values %}
<option value="{{ i.id }}">{{ i }}</option>
{% endfor %}
Expand All @@ -451,7 +453,7 @@ <h1 class="modal-title fs-5" id="addSalesLabel">Add {{ link }}</h1>
<label for="id_{{ field_name }}" class="form-label">{{ field_name|title }}</label>
{% if field_name in choices_dict %}
<select name="{{ field_name }}" id="id_{{ field_name }}" class="form-select">
<option value="">Select {{ field_name }}</option>
<option selected="selected" value="">Select {{ field_name }}</option>
{% for key, value in choices_dict|get(field_name) %}
<option value="{{ key }}">{{ key }}</option>
{% endfor %}
Expand Down Expand Up @@ -607,4 +609,4 @@ <h1 class="modal-title fs-5" id="addSalesLabel">Add {{ link }}</h1>

</script>

{% endblock extra_js %}
{% endblock extra_js %}