模型.py
class Decomposicao(models.Model):
tirosina = models.BooleanField('tirosina')
fenilalanina = models.BooleanField('fenilalanina')
class Meta:
abstract = True
class SDF(models.Model):
numero = models.IntegerField('SDF', unique=True, primary_key=True)
decomposicao = models.EmbeddedModelField(
model_container=Decomposicao,
)
data_insercao = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.numero)
视图.py
def search(request):
data = {}
if request.method == 'GET':
search = request.GET
search = search['sdf']
if search.startswith("SDF") or search.startswith("sdf"):
sdf = SDF.objects.get(pk=search[3:])
else:
sdf = SDF.objects.get(pk=search)
data['sdf'] = sdf
data['numero'] = format(sdf.numero, '04d')
return render(request, 'app/busca.html', data)
我正在使用 mongodb 和 django,所以我决定使用 djongo 作为连接器 - djongo doc - 据说我正在尝试显示我在 django 模板中的查询中找到的内容 - busca.html - 但我找不到一种显示嵌入式模型的方法。
busca.html
{% extends 'app/base.html' %}
{% block cabecalho %}
{% load staticfiles %}
<title>SDF{{ numero }}</title>
{% endblock%}
{% block conteudo %}
<section class="bg-light">
<div class="container ">
<div class="col-lg-12 h-100 text-center text-lg-left my-auto">
<h1 class="text-muted medium mb-4 mb-lg-0">SDF{{ numero }</h1>
<br>
{{ sdf }}
</div>
</div>
</section>
{% endblock %}
这样做只会显示 sdf 的数字 - 'numero'。
谢谢。