'Model' Object is not iterable

Asked

Viewed 1,301 times

2

As soon as I click on a button that calls my view to edit the Row of the table in question shows me the error: 'Meumodel' Object is not iterable.

  • I’m using Django 2.1.15
  • Python 3.8.1
  • SQL server database

py views.

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

    if(request.method == 'POST'):
        return False

    else:
        return render(request, 'portaria/cadastroPessoa.html', {'form': form, 'visitantes': visitantes})

Full error message:

TypeError at /portaria/alterarPessoa/18
'DadosPessoas' object is not iterable
Request Method: GET
Request URL:    http://127.0.0.1:8000/portaria/alterarPessoa/18
Django Version: 2.1.15
Exception Type: TypeError
Exception Value:    
'DadosPessoas' object is not iterable
Exception Location: C:\Users\leona\AppData\Roaming\Python\Python38\site-packages\django\template\defaulttags.py in render, line 165
Python Executable:  C:\Users\leona\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version: 3.8.1
Python Path:    
['C:\\Users\\leona\\OneDrive\\Ambiente de '
 'Trabalho\\projeto\\MCS_JSP\\mcs\\MCS_Back',
 'C:\\Users\\leona\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
 'C:\\Users\\leona\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
 'C:\\Users\\leona\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
 'C:\\Users\\leona\\AppData\\Local\\Programs\\Python\\Python38-32',
 'C:\\Users\\leona\\AppData\\Roaming\\Python\\Python38\\site-packages',
 'C:\\Users\\leona\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
Server time:    Sex, 10 Abr 2020 13:21:14 -0300


Error during template rendering
In template C:\Users\leona\OneDrive\Ambiente de Trabalho\projeto\MCS_JSP\mcs\MCS_Back\portaria\templates\portaria\cadastroPessoa.html, error at line 372

Linha 372: {% for visitante in visitantes %}
  • puts the complete error message and the line where it is accusing error

  • I added the message and the line

  • this error accuses that the visitors variable is not eternal, that is, python cannot run for loop on it. What is the type of visitor data? run a print(type(visitors))

1 answer

2

To popular the variable visitantes you’re using get_object_or_404() that behind the cloths will make a get() if you don’t find anything it will launch Http404 instead of Doesnotexist.

The problem is in get_object_or_404() use the get(). This is analogous to do:

DadosPessoas.objects.get(id=id)

The get() will bring an object of the type Data for he is doing a direct search for the id. The Dadospeople-like object is not, in fact, iterable.

It would have to be used then the filter() instead of get().

The filter() returns an object of type Queryset which in turn is iterable. Thus using the:

visitantes = get_list_or_404(DadosPessoas, pk=id)

Underneath the covers will be executed something like:

DadosPessoas.objects.filter(id=id)

Then returning an object with the right type. See the documentation for get_list_or_404() and examples.

If you are dealing with data from form the form of treatment is completely different and for this it is recommended the use of formsets.

  • I understood what was causing the error and even before testing it made sense to me and figured it would work, but when testing I got a new error: 'list' Object has no attribute '_meta'

  • What exactly does the Personal method do? If you are dealing with form data, the ideal is to use Formsets From the error you described it seems to be the case.

  • Yes form data, I will try to use Formsets so I don’t know how but I will try to learn

Browser other questions tagged

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