Django - How to present last value registered in the database in a registration screen

Asked

Viewed 54 times

0

I’m young, and I’m still facing difficulties that might seem pretty simple to you. I decided for personal reasons to make an application in Django. It is a simple registration of people in a Mysql database using Django adminLTE, adminLTE_theme:

in models py.

from django.db import models


class Pessoas(models.Model):
    nome = models.CharField(max_length=150, verbose_name='nome completo')
    registro = models.IntegerField(verbose_name='número do registro')
    
    class Meta:
        managed = True
        db_table = 'pessoas'
        verbose_name = 'Filiado'
        verbose_name_plural = 'Filiados'

    def __str__(self):
        return self.nome

in admin.py

from django.contrib import admin
from .models import Pessoas

@admin.register(Pessoas)
class PessoasAdmin(admin.ModelAdmin):
    list_display = ('nome', 'registro')

When accessing the registration page in the administrative area, I would like to have a line (Row, or anything) that shows the number of the last registered record.

I tried to make a copy of the template change_form.html and direct to this html:

from django.contrib import admin
from .models import Pessoas

@admin.register(Pessoas)
class PessoasAdmin(admin.ModelAdmin):
    list_display = ('nome', 'registro')
    add_form_template = 'cad_filiado.html'

and in html:

{% block field_sets %}
    <div class="row">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Pessoa</th>
                    <th>Registro</th>
                </tr>
            </thead>
            <tbody>
                {% for p in pessoas %}

                    <tr>
                        <td>{{ p.pessoa }}</td>
                        <td>{{ p.registro }}</td>
                    </tr>
                {% endfor %}

            </tbody>
        </table>
    </div>
{% endblock %}

I even copied the code from change_list_result.html in cad_affiliated.html and gives reverse_url error... tried several things, read and reread the documentation and found no answer. (Maybe the answer until it’s there, but I did not understand how to run)

You may wonder: why do you want this information on the registration page?

A: To maintain a pleasant usability, without having to search the records.

thank you for your patience.

1 answer

0


It was a very simple problem to solve but it had nothing to do with what I was reading to try to solve.

Basically: I created a tag and used the html copy of the template change_form.html who had made.

Step 1: I created a directory inside the app with the name templatetags

Step 2: I created a file inside the new directory called filiado_utilitario.py

Step 3: I did the imports, records and a database search:

from django import template

register = template.Library()


@register.simple_tag
def pesquisa_filiado():
    from django.db import connection
    with connection.cursor() as cursor:
        cursor.execute(""" SELECT registro FROM pessoas ORDER BY id DESC limit 1;""")
        ult_pessoa = cursor.fetchone()
    return ult_pessoa[0]

Step 4: in the cad_affiliated.html added the file import .py

{% load i18n admin_urls static admin_modify filiado_utilitario %}

Step 5: I went to the archive .virtualenvs/Meu_projeto_estudo/lib/python3.7/site-Packages/Django/contrib/admin/templates/admin/includes/fieldset.html and copied the excerpt:

<fieldset class="module aligned {{ fieldset.classes }}">
     <div class="form-row"> 
     </div>
</fieldset>

and entered the tag right after the start of the form:

<form {% if has_file_field %} enctype="multipart/form-data" {% endif %}
      action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form" novalidate>
    {% csrf_token %}
    {% block form_top %} 
<fieldset class="module aligned {{ fieldset.classes }}">
     <div class="form-row"> 
     Uĺtimo Filiado cadastrado número: {% pesquisa_filiado %}
     </div>
</fieldset>

and end.

You can brush a little more and I accept suggestions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.