首先是关于我的一些事情。我只是一个初学者Python和Django开发人员,所以我正在学习和应用我在实际工作项目中学到的东西。
问题所在
我正在为我工作的医院建立一份小药房管理应用程序,我想要制作一份药品供应追踪表,医生可以检查一种药物是否可用,以便他们可以为病人开出处方。
我做了什么?
我有“国家药品配方汇编”的表格模型,这是我国(古巴)使用的所有药物和药理学产品的完整清单,在我的Django models.py中描述为:
class FarmMedicamento(models.Model):
meddesc = models.CharField(max_length=250)
presentacion = models.ForeignKey(FarmPresentacion, models.DO_NOTHING)
forma_presentacion = models.CharField(max_length=250, blank=True, null=True)
def __str__(self):
return ("%s - %s %s" % (self.meddesc, str(self.presentacion).upper(), self.forma_presentacion))
def propiedad_colname(self):
return ("%s - %s %s" % (self.meddesc, str(self.presentacion).upper(), self.forma_presentacion))
propiedad_colname.short_description = 'Medicamento'
columna_medicamento = property(propiedad_colname)
class Meta:
managed = False
db_table = 'farm_medicamento'
verbose_name = "Medicamento"
verbose_name_plural = 'Medicamentos'我将药物可用性存储在另一个表模型中,描述为:
class FarmExistencia(models.Model):
medid = models.OneToOneField(FarmMedicamento, models.DO_NOTHING, primary_key=True, verbose_name="Medicamento")
disponible = models.BooleanField("Disponible")
actualizado = models.DateTimeField(auto_now=True)
class Meta:
managed = True
db_table = 'farm_existencia'
verbose_name = "Disponibilidad de medicamento"
verbose_name_plural = 'Disponibilidad de medicamentos'
def __str__(self):
return ("%s" % (self.medid))医学清单视图如下:
class FarmMedicamentoListView(LoginRequiredMixin, ListView):
model = FarmMedicamento
context_object_name = 'listado_fnm'
template_name = 'existencias/farmmedicamento_list.html'
def get_context_data(self, **kwargs):
context = super(FarmMedicamentoListView, self).get_context_data(**kwargs)
context['disponibles'] = FarmExistencia.disponibles.all()
context['agotados'] = FarmExistencia.agotados.all()
return context我想要什么?
我想添加新的药物,按下主列表中的加号按钮,并立即更新“可用药物”的绿色分区,但我不知道如何做到。
我的实际模板列表的屏幕截图在:https://i.imgur.com/yNr7jic.png
模板HTML
{% extends 'base.html' %}
{% load static %}
{% block title %}AllenFarma | Listado de medicamentos {% endblock title %}
{% block head %}
<link href="{% static 'css/datatables.min.css' %}" rel="stylesheet" type="text/css" />
<script src="{% static 'js/datatables.min.js' %}"></script>
<style rel="stylesheet" type="text/css">
.table > tbody > tr > td.item_add-control {
background: url('/static/images/icons/add_button.png') no-repeat center center;
cursor: pointer;
padding: 8px 12px 8px 12px;
}
.table > tbody > tr > td.item_remove-control {
background: url('/static/images/icons/remove_button.png') no-repeat center center;
cursor: pointer;
padding: 8px 12px 8px 12px;
}
</style>
{% endblock head %}
{% block content %}
<div class="container">
<div class="col-md-6">
<div class="panel panel-success">
<div class="panel-heading">
<div style="text-align:left" class="panel-title">Medicamentos en existencia</div>
</div>
<div style="padding-top:10px" class="panel-body">
{% include 'existencias_list.html' %}
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-danger">
<div class="panel-heading">
<div style="text-align:left" class="panel-title">Medicamentos agotados</div>
</div>
<div style="padding-top:10px" class="panel-body">
{% include 'agotados_list.html' %}
</div>
</div>
</div>
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading">
<div style="text-align:left" class="panel-title">Medicamentos del Formulario Nacional de Medicamentos</div>
</div>
<div style="padding-top:10px" class="panel-body">
<table id="listado_medicametos" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Medicamentos</th>
<th>Presentación</th>
<th>Forma de presentación</th>
<th></th>
</tr>
</thead>
<tbody>
{% for medicamento in listado_fnm %}
<tr>
<td>{{medicamento.meddesc}}</td>
<td>{{medicamento.presentacion}}</td>
<td>{{medicamento.forma_presentacion}}</td>
<td id="add_control"></td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready( function () {
$('#listado_medicametos')
.dataTable({
"columns": [
{"data": "meddesc"},
{"data": "presentacion"},
{"data": "forma_presentacion"},
{
"class": "item_add-control",
"orderable": false,
"data": null,
"defaultContent": ""
},
],
"order": [[0, 'asc']],
"language": {
"url": "/static/i18n/es_ES.json"
},
scrollY: 230
});
});
</script>
{% endblock content %}发布于 2019-05-10 11:15:44
解决此类问题的基本工作流可能是Ajax调用,将更改发布到Django服务器,Django视图用于在模型中接收和实现更改,并响应Ajax调用,Ajax调用将根据所做的任何更新更新HTML。
一个简单的例子:
Javascript:
您需要一个Ajax方法,当您单击add/remove按钮并将药物的ID发布到Django视图时触发它,在那里您可以将其设置为可用/不可用。
$('.addButton').on('click', function(event){
$.ajax({
url : '/add/medicine/url/',
type : 'POST',
data : {'medicine_id': id, csrfmiddlewaretoken: 'your csrftoken' },
success: function(json) {
// Successful response, so you can update the medicine list HTML using JQuery here.
},
});
});views.py
from django.http import JsonResponse
def add_medicine(request):
if request.method == 'POST':
medicine_id = int(request.POST.get('medicine_id'))
medicine = Medicine.objects.get(id=medicine_id)
medicine.available = True
medicine.save()
return JsonResponse({'medicine_id': medicine_id}, status=200)这是一个说明工作流的例子,命名与您的代码不匹配。另外,我认为您应该在药物模型本身中指定药物可用性,而不是在两个单独的模型中,或者至少在这两个模型之间有一些反向参考关系,这样您就可以通过另一个模型访问其中一个模型,反之亦然。
发布于 2019-04-16 19:45:21
因为您希望通过管理界面添加新药并自动刷新列表,所以必须编写Javascript函数来加载数据并填充可用药物列表。
应该周期性地执行JS函数(使用计时器),并调用Django视图,该视图提供您想要在其中显示的数据。
https://stackoverflow.com/questions/55715030
复制相似问题