user8504021
user8504021

Reputation: 283

AttributeError at /app/api/get

I got an error, AttributeError at /app/api/get Got AttributeError when attempting to get a value for field task_name on serializer TaskSerializer. The serializer field might be named incorrectly and not match any attribute or key on the Color instance. Original exception text was: 'Color' object has no attribute 'task_name'.

Now I wanna make a page that shows model's content in json format. models.py is

from django.db import models

# Create your models here.
class Color(models.Model):
    name = models.CharField(max_length=255)
    background_color = models.CharField(max_length=255)
    h1_color = models.CharField(max_length=255)
    p_color = models.CharField(max_length=255)

    def __str__(self):
        return self.name

serializers.py is

from .models import Color
from rest_framework import serializers

class TaskSerializer(serializers.Serializer):
    task_name = serializers.CharField(max_length=100)
    status = serializers.SerializerMethodField('get_task_status')

    def get_task_status(self, instance):
        return instance.status.status

    class Meta:
        model = Color
        fields = ('name',
                  'background_color',
                  'h1_color',
                  'p_color',
                  'task_name')

urls.py is

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'api/get',views.TaskGet.as_view(),name='task-get')
]

views.py is

from django.shortcuts import render
from .models import Color
from .forms import ColorForm
from .serializers import TaskSerializer
from rest_framework.views import APIView
from rest_framework.response import Response

from rest_framework import status

# Create your views here.
def index(request):
    d = {
        'colors': Color.objects.all(),
        'form': ColorForm(),
    }
    return render(request, 'index.html', d)

class TaskGet(APIView):
    def get(self, request, format=None):
        obj = Color.objects.all()
        serializers = TaskSerializer(obj, many=True)
        return Response(serializers.data, status.HTTP_200_OK)

I wrote url(r'api/get',views.TaskGet.as_view(),name='task-get') in urls.py,so I really cannot understand why this error happens.I already run commands of migration of model. How can I fix this? My ideal web page is like ideal page

Upvotes: 0

Views: 90

Answers (1)

user8060120
user8060120

Reputation:

You try get status by foreign key instance.status.status but in your model class Color i don't see any foreign keys or methods for it.

And for task_name did you want to see the model field name try to add source params

task_name = serializers.CharField(max_length=100, source='name')
#                                                   ^^^^^^^^^

are you sure you want serialize Task for model Color?

new edit

in your get_task_status the 'instanceis instance of serializer model, so if your modelColorhas no property or methodstatus` you will catch an error

Upvotes: 1

Related Questions