Attributeerror: 'list' Object has no attribute '_meta'

Asked

Viewed 900 times

-1

I’m trying to edit an object but I’m not being able to instantiate it by the desired id filter to show in my form and edit.

Django 2.1.15 Python 3.8.1

my views.py

def Alterar_Pessoa(request, id):
    visitantes = get_list_or_404(DadosPessoas, id=id)
    form = PessoaForm(instance=visitantes)

    if(request.method == 'POST'):
        form = PessoaForm(request.POST, instance=visitantes)
        if(form.is_valid()):
            form.save()
            return redirect('cadastroPessoa')
    context = {
        'forms': form,
        'visitantes': visitantes,
    }
    return render(request, 'portaria/cadastroPessoa.html', context)

excerpt from the template that loads the form and makes the request cadasPessoa.html

{% for visitante in visitantes %}
      <tr>
         <td><a href="#">{{visitante.nome}}</a></td>
         <td>{{visitante.empresa}}</td>
         <td>{{visitante.tipo}}</td>

          <td><a type="button" class="btn mb-1 btn-danger"
           style="position: relative;"href="excluirPessoa/{{visitante.id}}">Excluir<span class="btn-icon-right"><i
                                                            class="fa fa-close"></i></span>
                                                            </a>
                                                            <a style="color: white;" type="button" href="alterarPessoa/{{visitante.id}}"
                                                        class="btn mb-1 btn-secondary"
                                                        style="position: absolute;">Editar<span
                                                            class="btn-icon-right"><i class="icon-note"></i></span>
                                                        </a>
                                                        </td>


                                        </tr>
                                        {% endfor %}

py.

from django.urls import path, include
from portaria import views

urlpatterns = [
    path('', views.Pagina_Login, name='paginaLogin'),
    path('mcs/home', include('portaria.urls')),
    path('portaria/cadastroPessoa', views.Cadastro_Pessoa, name="cadastroPessoa"), 
    path('portaria/cadastroVeiculo', views.Cadastro_Veiculo, name="cadastroVeiculo"),
    path('portaria/cadastroCredencial', views.Cadastro_Credencial, name="cadastroCredencial"),
    path('portaria/movi-Entrada', views.Movi_Entrada, name="movi-Entrada"), 
    path('portaria/telaBloqueio', views.Tela_Bloqueio, name='telaBloqueio'),
    path('portaria/excluirPessoa/<int:id>', views.Excluir_Pessoa, name="excluirPessoa"),
    path('portaria/alterarPessoa/<int:id>', views.Alterar_Pessoa, name="alterarPessoa"),
]

Traceback of error

File "C: Users loliveira.JSP Appdata Local Programs Python Python38-32 lib site-Packages Django core handlers Exception.py", line 34, in Inner Response = get_response(request) File "C: Users loliveira.JSP Appdata Local Programs Python Python38-32 lib site-Packages Django core handlers base.py", line 126, in _get_response Response = self.process_exception_by_middleware(e, request) File "C: Users loliveira.JSP Appdata Local Programs Python Python38-32 lib site-Packages Django core handlers base.py", line 124, in _get_response Response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C: Users loliveira.JSP Desktop projectsp MCS Mcs_back port views.py", line 29, in Alterar_pessoa form = Personal form(instance=visitors) File "C: Users loliveira.JSP Appdata Local Programs Python Python38-32 lib site-Packages Django Forms models.py", line 292, in init object_data = model_to_dict(instance, opts.Fields, opts.exclude) File "C: Users loliveira.JSP Appdata Local Programs Python Python38-32 lib site-Packages Django Forms models.py", line 82, in model_to_dict opts = instance. _meta Attributeerror: 'list' Object has no attribute '_meta' [23/Apr/2020 11:33:49] "GET /ordinance/alterarPessoa/5 HTTP/1.1" 500 78437

Note: I used the same structure but using get_objects_or_404(Model, id=id) earlier but did not give the 'model' error Objects is not iterable, and this get method worked normally when deleting a record.

1 answer

1


You’re trying to change one object, one person, but you’re searching for a list from the bank:

visitantes = get_list_or_404(DadosPessoas, id=id)

For django documentation, the method get_list_or_404() returns a list of results. To return only the result corresponding to the object with the specified id, use the method get_object_or_404 (more about him here).

visitantes = get_object_or_404(DadosPessoas, id=id)
  • I tried to use get_object_or_404 too, but it brought me an iteration error saying that the object is not iterable, it was after receiving this error that I was suggested to use the list .

Browser other questions tagged

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