models.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)
views.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)
I'm using mongodb and django, so I decided to utilize djongo as the connector - djongo doc - that been said I'm trying to display the content I find on querys in django templates - busca.html - but I can't find a way to display the Embedded models.
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 %}
Doing that only display the number - 'numero' - of the sdf.
Thanks.
{{ sdf }}
calls the __str__
method of your model, which returns numero as defined. To display all the fields use {{ sdf.numero }}
, {{ sdf.decomposicao }}
, {{ sdf.data_insercao }}
. I believe that you could access fields of your embedded model with dot notation, for example {{ sdf.decomposicao.tirosina }}
.