Can't display embedded models on templates
One line description of the issue
I can't display embedded models on templates
Python script
view
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)
model
from djongo import models
from django import forms
class U_Carboidratos(models.Model):
glicose = models.BooleanField('glicose')
arabinose = models.BooleanField('arabinose')
manitol = models.BooleanField('manitol')
xilose = models.BooleanField('xilose')
lactose = models.BooleanField('lactose')
sacarose = models.BooleanField('sacarose')
class Meta:
abstract = True
class Hidrolise(models.Model):
amido = models.BooleanField('amido')
caseina = models.BooleanField('caseina')
gelatina = models.BooleanField('gelatina')
class Meta:
abstract = True
class Crescimento(models.Model):
nacl2 = models.BooleanField('nacl2')
nacl5 = models.BooleanField('nacl5')
nacl7 = models.BooleanField('nacl7')
nacl10 = models.BooleanField('nacl10')
anaerobios = models.BooleanField('anaerobios')
sabourand = models.BooleanField('sabourand')
class Meta:
abstract = True
class Producao(models.Model):
acetoina = models.BooleanField('acetoina')
nitrito = models.BooleanField('nitrito')
indol = models.BooleanField('indol')
catalase = models.BooleanField('catalase')
oxidase = models.BooleanField('oxidase')
class Meta:
abstract = True
class Utilizacao(models.Model):
citrato = models.BooleanField('citrato')
propionato = models.BooleanField('propionato')
esculina = models.BooleanField('esculina')
class Meta:
abstract = True
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)
u_carboidratos = models.EmbeddedModelField(
model_container=U_Carboidratos,
)
hidrolise = models.EmbeddedModelField(
model_container=Hidrolise,
)
crescimento = models.EmbeddedModelField(
model_container=Crescimento,
)
producao = models.EmbeddedModelField(
model_container=Producao,
)
utilizacao = models.EmbeddedModelField(
model_container=Utilizacao,
)
decomposicao = models.EmbeddedModelField(
model_container=Decomposicao,
)
data_insercao = models.DateTimeField(auto_now_add=True)
objects = models.DjongoManager()
def __str__(self):
return str(self.numero)
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.hidrolise }}
</div>
</div>
</section>
{% endblock %}
Traceback
Environment:
Request Method: GET Request URL: http://127.0.0.1:8000/busca/?sdf=2
Django Version: 2.1.1 Python Version: 3.7.0 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error: In template /home/vitordullens/Documentos/pibic/cbafes/cbafes/app/templates/app/base.html, error at line 0 'NoneType' object has no attribute 'attname' 1 : 2 : 3 : {% load staticfiles %} 4 : 5 : 6 :
7 : 8 : 9 : 10 :Traceback:
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner 34. response = get_response(request)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response 126. response = self.process_exception_by_middleware(e, request)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response 124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vitordullens/Documentos/pibic/cbafes/cbafes/app/views.py" in search 30. return render(request, 'app/busca.html', data)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/shortcuts.py" in render 36. content = loader.render_to_string(template_name, context, request, using=using)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/loader.py" in render_to_string 62. return template.render(context, request)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/backends/django.py" in render 61. return self.template.render(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in render 171. return self._render(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in _render 163. return self.nodelist.render(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in render 937. bit = node.render_annotated(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in render_annotated 904. return self.render(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/loader_tags.py" in render 150. return compiled_parent._render(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in _render 163. return self.nodelist.render(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in render 937. bit = node.render_annotated(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in render_annotated 904. return self.render(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/loader_tags.py" in render 62. result = block.nodelist.render(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in render 937. bit = node.render_annotated(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in render_annotated 904. return self.render(context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in render 993. return render_value_in_context(output, context)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/template/base.py" in render_value_in_context 972. value = str(value)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/db/models/base.py" in str 506. return '%s object (%s)' % (self.class.name, self.pk)
File "/home/vitordullens/Documentos/pibic/cbafes/venv/lib/python3.7/site-packages/django/db/models/base.py" in _get_pk_val 553. return getattr(self, meta.pk.attname)
Exception Type: AttributeError at /busca/ Exception Value: 'NoneType' object has no attribute 'attname'
You need to serialize the embedded field data first. You may try my solution: https://github.com/nesdis/djongo/issues/115#issuecomment-384910745
Issue has be solved in #115