Home > OS >  I am getting this error: AttributeError at /home/ 'list' object has no attribute 'val
I am getting this error: AttributeError at /home/ 'list' object has no attribute 'val

Time:09-29

models.py

from django.db import models

class Student(models.Model):

    Student_Name           =  models.CharField(max_length=40)
    Father_Name            =  models.CharField(max_length=40)
    Contact                =  models.CharField(max_length=10)
    Address                =  models.CharField(max_length=120)

    def __str__(self):
        return self.Student_Name

serializers.py

from rest_framework import serializers

from .models import Student


class StudentForm(serializers.ModelSerializer):
    
    model = Student
    fields = ['Student_Name','Father_Name','Contact','Address']

app urls.py

from django.urls import path

from restapiapp import views


urlpatterns = [
    path('home/', views.HomeView, name='home'),
]

admin.py

from django.contrib import admin

from .models import Student

admin.site.register(Student)

views.py

from django.http import HttpResponse,JsonResponse

from rest_framework.decorators import api_view

from .models import Student

from .serializers import StudentForm

@api_view(['GET'])

def HomeView(request):
    
    serializer_obj = Student.objects.all()
    serializer1     = StudentForm(serializer_obj,many=True)
    return JsonResponse(serializer1.data, safe=False)

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'restapiapp',
]

urls.py

from django.urls import path, include

from django.contrib import admin

urlpatterns = [

    path('admin/', admin.site.urls),

    path('', include('restapiapp.urls')),
]

Error:

Internal Server Error: /home/
Traceback (most recent call last):
 File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
 response = get_response(request)
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\django\views\generic\base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
 response = self.handle_exception(exc)
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
  self.raise_uncaught_exception(exc)
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
   raise exc
 File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\decorators.py", line 50, in handler
        return func(*args, **kwargs)
File "C:\Users\Omkar\django_projects\restapipro1\restapiapp\views.py", line 12, in HomeView
        return JsonResponse(serializer1.data, safe=False)
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\serializers.py", line 768, in data
        ret = super().data
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\serializers.py", line 253, in data
        self._data = self.to_representation(self.instance)
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\serializers.py", line 686, in to_representation
        return [
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\serializers.py", line 687, in <listcomp>
        self.child.to_representation(item) for item in iterable
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\serializers.py", line 507, in to_representation
        for field in fields:
File "C:\Users\Omkar\django_projects\myenv\lib\site-packages\rest_framework\serializers.py", line 368, in _readable_fields
        for field in self.fields.values():
    AttributeError: 'list' object has no attribute 'values'
    [28/Sep/2022 23:38:46] "GET /home/ HTTP/1.1" 500 116700

I am getting this error in the console. What do I need to do?

CodePudding user response:

in your serializer add class meta like this :

from rest_framework import serializers

from .models import Student


class StudentForm(serializers.ModelSerializer):
    class Meta: 
       model = Student
       fields = ['Student_Name','Father_Name','Contact','Address']

CodePudding user response:

yes correct brother i have forgotten to write that it was my fault

  • Related