Here is the error when I go to employees page (employees is the model):
TypeError at /employees/
'method' object is not iterable
Request Method: GET
Request URL: http://127.0.0.1:8000/employees/
Django Version: 3.2
Exception Type: TypeError
Exception Value:
'method' object is not iterable
Exception Location: C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\rest_framework\serializers.py, line 677, in to_representation
Python Executable: C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\Scripts\python.exe
Python Version: 3.9.4
Python Path:
['C:\\Users\\USER\\PycharmProjects\\djangoProject\\djangoProject\\djangoProject\\djangoProject\\djangoProject\\djangoProject1\\health_recommender_final',
'C:\\Users\\USER\\PycharmProjects\\djangoProject\\djangoProject\\djangoProject\\djangoProject\\djangoProject\\djangoProject1\\health_recommender_final',
'C:\\Program Files\\JetBrains\\PyCharm '
'2021.1.1\\plugins\\python\\helpers\\pycharm_display',
'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\USER\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\USER\\PycharmProjects\\djangoProject\\djangoProject\\djangoProject\\djangoProject\\djangoProject\\djangoProject1\\health_recommender_final\\venv',
'C:\\Users\\USER\\PycharmProjects\\djangoProject\\djangoProject\\djangoProject\\djangoProject\\djangoProject\\djangoProject1\\health_recommender_final\\venv\\lib\\site-packages',
'C:\\Program Files\\JetBrains\\PyCharm '
'2021.1.1\\plugins\\python\\helpers\\pycharm_matplotlib_backend']
Server time: Fri, 18 Jun 2021 01:49:34 +0000
My main files are serializers and models.
from rest_framework import serializers
from rest_framework import Person
class PersonSerializer(serializers.ModelSerializer):
class Meta:
models = Person
fields = '__all__'
from django.db import models
# Create your models here.
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Person(models.Model):
EDUCATION_CHOICES = (
('NA', 'NONE'),
('SSC', 'SSC'),
('HSC', 'HSC'),
('undergraduate', 'UNDERGRADUATE'),
('graduate', 'POSTGRADUATE'),
('Further Studies or PhD', 'FURTHER/PhD'),
)
DIET = (
('Vegetarian', 'VEG'),
('Omnivore', 'OMNI'),
('Fish', 'FISH'),
)
EXERCISE = (
('na', 'NA'),
('yes', 'YES'),
('no', 'NO'),
)
name = models.CharField(max_length=250,default="")
education_details = models.CharField(max_length=150, choices=EDUCATION_CHOICES, default='NA')
diet = models.CharField(max_length=80, choices=DIET, default='VEG')
exercise = models.CharField(max_length=80, choices=EXERCISE, default='NA')
class Meta:
ordering = ('-name',)
@property
def __str__(self):
return self.name
So what should I do now? I think the method that is not iterable is coming because I left something out somewhere, where can it be?
I am trying to create a small app, which when taken has the above input as the model, which returns a JSON file. After that I would manipulate the JSON file with some programming to create health recommendations based on lifestyle choice. Can someone tell me where I am going wrong and how I can complete the JSON manipulation thing to get simple for - if submission things to create the recommendations??
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Person
from .serializers import PersonSerializer
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Person
from .serializers import PersonSerializer
class PersonList(APIView):
def get(self, request):
person1 = Person.objects.all
serializer = PersonSerializer(person1, many=True)
return Response(serializer.data)
def post(self):
pass
urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from healthrecomm import views
urlpatterns = [
path('admin/', admin.site.urls),
path('employees/', views.PersonList.as_view())
]
System check identified no issues (0 silenced).
June 18, 2021 - 07:49:18
Django version 3.2, using settings 'health_recommender_final.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[18/Jun/2021 07:49:34] "GET /employees HTTP/1.1" 301 0
Internal Server Error: /employees/
Traceback (most recent call last):
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\rest_framework\views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
raise exc
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\rest_framework\views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\healthrecomm\views.py", line 23, in get
return Response(serializer.data)
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\rest_framework\serializers.py", line 760, in data
ret = super().data
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\rest_framework\serializers.py", line 260, in data
self._data = self.to_representation(self.instance)
File "C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\venv\lib\site-packages\rest_framework\serializers.py", line 677, in to_representation
return [
TypeError: 'method' object is not iterable
[18/Jun/2021 07:49:34] "GET /employees/ HTTP/1.1" 500 105485
Not Found: /favicon.ico
[18/Jun/2021 07:49:35] "GET /favicon.ico HTTP/1.1" 404 2123
C:\Users\USER\PycharmProjects\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject\djangoProject1\health_recommender_final\healthrecomm\models.py changed, reloading.
Watching for file changes with StatReloader