1

I'm creating a simple blog application and I am trying to convert django rest api. But, I got this error

TypeError at /user/api/ hasattr(): attribute name must be string

Exception Type: TypeError at /user/api/

Exception Value: hasattr(): attribute name must be string

this is my models.py file

from django.conf import settings
from django.db import models
from django.urls import reverse

class BlogPost(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    blog_title=models.CharField(max_length=200)
    blog_description=models.TextField()
    blog_pub=models.DateTimeField(auto_now_add=True)
    blog_update=models.DateTimeField(auto_now=True)


    def __str__(self):
        return self.blog_title


    def get_absolute_url(self):

        return reverse('blog:blog_post', kwargs={'pk': self.pk})

this is my serializers.py file

from rest_framework import serializers

from blog.models import BlogPost


class BlogPostSerializer(serializers.ModelSerializer):

    class Meta:
        fields=(
            'author',
            'blog_title',
            'blog_description',
        ),
        model=BlogPost

This is views.py file

from blog.models import BlogPost

from .serializers import BlogPostSerializer
from rest_framework import generics
from . import serializers
from . import serializers


class BlogPostListAPI(generics.ListCreateAPIView):
    queryset=BlogPost.objects.all()
    serializer_class=BlogPostSerializer


class BlogPostListAPIDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = BlogPost.objects.all()
    serializer_class = serializers.BlogPostSerializer

this is urls.py file

from django.urls import path,include

from .views import SignUpForm, UserProfiles
from .api.views import *


urlpatterns = [
    path('signup/',  SignUpForm.as_view(), name='signup'),
    path('profile/', UserProfiles.as_view(), name='profile'),
    path('api/', BlogPostListAPI.as_view(), name='asad'),
    path('api/<int:pk>', BlogPostListAPIDetail.as_view()),
]

this is screenshot enter image description here

5
  • 1
    can't see where hasttr is used Commented Apr 21, 2018 at 8:30
  • All code shared. but I don't know how can I get this error. Also, I didn't use hasttr. Commented Apr 21, 2018 at 8:34
  • click the switch to copy and paste view and paste in that error in the updated error. should likely list then where it crashed. Commented Apr 21, 2018 at 8:36
  • 4
    Remove Comma ',' just before model in the BlogPostSerializer Commented Apr 21, 2018 at 8:43
  • You are right, MaNKuR Commented Apr 21, 2018 at 14:40

1 Answer 1

4

As @MaNKuR pointed out in a comment to the question, you have an additional comma in your serializer definition at the end of Meta.fields:

class BlogPostSerializer(serializers.ModelSerializer):

    class Meta:
        fields=(
            'author',
            'blog_title',
            'blog_description',
        ),

Because of this, Meta.fields is now a "tuple of tuple of strings" instead of just a "tuple of strings".

When iterating over fields, instead of the expected three string elements

1. 'author'
2. 'blog_title'
3. 'blog_description'

you get only one element that is not a string

1. ('author', 'blog_title', 'blog_description')

Hence, the internal code of the Serializer complains that it did not get a string like it is supposed to get.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.