I am a novice programmer, I am learning python and Django on my own. I got stuck in a part of my application:
I have many models with many attributes, and I have a window and a template for each of them. The idea is to show in each one, a table with all its content with an sql query in the file view.py
objects.all ()
My code:
MODELS.PY
from django.db import models
class MFuel(models.Model):
marca = models.TextField(db_column='Marca', blank=True, null=True) # Field name made lowercase.
modelo = models.TextField(db_column='Modelo', blank=True, null=True) # Field name made lowercase.
potencia_electrica_kw = models.BigIntegerField(db_column='Potencia_electrica_kW', blank=True, null=True) # Field name made lowercase.
fecha_oferta = models.DateTimeField(db_column='Fecha_oferta', blank=True, null=True) # Field name made lowercase.
pais_proyecto = models.TextField(db_column='Pais_Proyecto', blank=True, null=True) # Field name made lowercase.
proyecto = models.TextField(db_column='Proyecto', blank=True, null=True) # Field name made lowercase.
uds = models.FloatField(db_column='Uds', blank=True, null=True) # Field name made lowercase.
precio_eur = models.FloatField(db_column='Precio_EUR', blank=True, null=True) # Field name made lowercase.
precio_usd = models.FloatField(db_column='Precio_USD', blank=True, null=True) # Field name made lowercase.
precio_unitario_eur = models.FloatField(db_column='Precio_Unitario_EUR', blank=True, null=True) # Field name made lowercase.
ratio_eur_kw = models.FloatField(db_column='Ratio_eur_KW', blank=True, null=True) # Field name made lowercase.
largo_mm = models.FloatField(db_column='Largo_mm', blank=True, null=True) # Field name made lowercase.
ancho_mm = models.FloatField(db_column='Ancho_mm', blank=True, null=True) # Field name made lowercase.
alto_mm = models.FloatField(db_column='Alto_mm', blank=True, null=True) # Field name made lowercase.
peso_kg = models.FloatField(db_column='Peso_kg', blank=True, null=True) # Field name made lowercase.
presupuesto = models.TextField(db_column='Presupuesto', blank=True, null=True) # Field name made lowercase.
esp_tecnicas = models.TextField(db_column='Esp_Tecnicas', blank=True, null=True) # Field name made lowercase.
observaciones = models.FloatField(db_column='OBSERVACIONES', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'm_fuel'
VIEWS.PY
def m_fuel(request):
fuel = MFuel.objects.all()
# QuerySet
myFilter = Filter(request.GET, queryset=fuel)
fuel = myFilter.qs
total = fuel.count()
attr = MFuel.__doc__
inici = attr.index("(") + 1
fi = attr.index(")")
nom_columnes = attr[inici:fi]
nom_columnes = str(nom_columnes)
nom_columnes = nom_columnes.split(",")
dict = {
"fuel": fuel,
"filter": myFilter,
"motores": motores_list,
"total": total,
"nom_columnes": nom_columnes,
}
return render(request, "motores/fuel.html", dict)
TEMPLATE.HTML
<table class="table_info">
<tr>
{% for nom in nom_columnes %}
<th>{{nom}}</th>
{% endfor %}
</tr>
{% for i in fuel %}
<tr>
<td>{{i.id}}</td>
<td>{{i.marca}}</td>
<td>{{i.modelo}}</td>
<td>{{i.potencia_electrica_kw}}</td>
<td>{{i.fecha_oferta}}</td>
<td>{{i.pais_proyecto}}</td>
<td>{{i.proyecto}}</td>
<td>{{i.uds}}</td>
<td>{{i.precio_eur}}</td>
<td>{{i.largo_mm}}</td>
<td>{{i.ancho_mm}}</td>
<td>{{i.alto_mm}}</td>
<td>{{i.peso_kg}}</td>
<td>{{i.presupuesto}}</td>
</tr>
{% endfor %}
</table>
My idea is to automate the table columns by accessing all the attributes of the class. I tried pulling out a list of the attribute names
attr = MFuel .__ doc__
and print them as follows:
<table class="table_info">
<tr>
{% for nom in nom_columnes %}
<th>{{nom}}</th>
{% endfor %}
</tr>
{% for i in fuel %}
<tr>
{% for nom in nom_columnes %}
<td>{{i.nom}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
but this shows nothing: capture that this code shows
It seems that it does not recognize the name as an attribute of the class, it must be because it is a string?
I saw this question, but I don't understand it Django - print all objects attribute values
Any help to automate this? Thanks a lot