1024 TIL

looggi·2022년 10월 24일
1

스파르타 내배캠 AI-3

목록 보기
46/130
post-thumbnail

🎃 장고 심화 1주차 ~

  • serialize : 직렬화
    아티클 모델(오브젝트형식)->제이슨 형식(스트링): 시리얼라이즈

  • null=True : db가 비어있어도 되는지

  • blank=True : 검증과정에서 비어있어도 유효하다고 판단할 것인지

  • return Response() 하면 아래 사진에서 상태 아래 아무런 정보도 나오지 않고
    return Response(serializer.data) 하면 완성된? 정보가 보여짐

  • Django's built-in serializers can only serialize querysets filled with django objects


🍭 오류

❌.accepted_renderer not set on Response

➡ If you're using a function based view, then this issue usually means you forgot to add the @api_view and the @renderer_classes decorator to your view.
➡ from rest_framework.decorators import api_view, @api_view 추가

❌AttributeError at /articles/index/
Got AttributeError when attempting to get a value for field title on serializer ArticleSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the QuerySet instance.
Original exception text was: 'QuerySet' object has no attribute 'title'.

➡ serializer = ArticleSerializer(articles, many=True) 추가(여러개니까 리스트 형태로 알아서 바꿔줌)

❌Assertion Error
You must call '.is_valid()' before calling '.save()'

➡ 시리얼라이저 객체 저장 전에 유효한지 검사 = 유효성 검사(모델 필드에서 건 여러 조건들에 잘 부합하는지, 필요한 값들이 모두 들어와있는지)를 먼저 해야함
if serializer.is_valid():
serializer.save()
else: print(serializer.errors)

❌Assertion Error
Cannot call '.is_valid()' as no 'data=' keyword argument was passed when instantiating the serializer instance

➡ 시리얼라이져 인스턴스를 만들 때 'data = ' 이라는 키워드가 누락됨
➡ serializer = ArticleSerializer(data = request.data)

❌"detail": "JSON parse error - Expecting property name enclosed in double quotes: line 5 column 5 (char 49)"
{
"title": "슬터",

}
➡ 쉼표 제거

❌IntegrityError at /articles/index/
NOT NULL constraint failed: articles_article.content
➡ 텍스트 필드에 null=True, blank=True 조건을 추가해줫으나 마이그레이션을 안해서.. 😞😲

❌TypeError at /articles/1/
Object of type Article is not JSON serializable
➡ 하나만 가지고 오는거니까..... 안해도 되지 않을까 생각했는데 오류가 난다
article= Article.objects.get(id=article_id)
아래에
serializer = ArticleSerializer(article) 추가해서 객체를 직렬화해준다

❌AssertionError
You passed a Serializer instance as data, but probably meant to pass serialized .data or .error. representation.
➡ return Response(serializer)
➡ return Response(serializer.data)

❌Page not found (404)
path('/', views.index, name='index'),
➡ path('', views.index, name='index'),


profile
looooggi

0개의 댓글