python/Django
[Django] ValueError - Cannot assign "'1'": "Board.member_no" must be a "Member" instance. 외래키가 있는 칼럼에서 글생성시 에러메세지
http://portfolio.wonpaper.net
2022. 5. 6. 01:06
Member 상에 no 라는 PK 가 있고, Board 상에 이와 연결된 member_no 라는 외래키 FK 가 있을때,
Board 글 삽입시 아래와 같은 에러메세지를 만났다.
ValueError at /board/write/
Cannot assign "'1'": "Board.member_no" must be a "Member" instance.
Request Method: | POST |
Request URL: | http://127.0.0.1:8000/board/write/ |
Django Version: | 4.0.4 |
Exception Type: | ValueError |
Exception Value: | Cannot assign "'1'": "Board.member_no" must be a "Member" instance. |
Exception Location: | /Users/jongwon/PycharmProjects/django_test2/venv/lib/python3.10/site-packages/django/db/models/fields/related_descriptors.py, line 235, in __set__ |
Python Executable: | /Users/jongwon/PycharmProjects/django_test2/venv/bin/python |
Python Version: | 3.10.4 |
Python Path: | ['/Users/jongwon/PycharmProjects/django_test2', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload', '/Users/jongwon/PycharmProjects/django_test2/venv/lib/python3.10/site-packages'] |
Server time: | Thu, 05 May 2022 23:40:25 +0900 |
위의 에러 메세지를 보면 ORM 프로그램이라서, 막연히 member_no 를 POST 결과 값으로 받아서 아래와 같이 Insert 할때 에러가 났다.
1
2
|
board = Board(member_no=member_no, subject=subject, content=content, read_num=0, image1=image1, upfile1=upfile1)
board.save()
|
cs |
아래와 같이 member_no 부분을 변경하도록 하자
1
2
|
board = Board(member_no=Member.objects.get(pk=member_no), subject=subject, content=content, read_num=0, image1=image1, upfile1=upfile1)
board.save()
|
cs |
https://stackoverflow.com/questions/37839867/django-error-cannot-assign-must-be-an-instance