페이징 질문 합니다.
>>> for i in range(300):
...     q = Question(subject='테스트:[%03d]' % i, content='내용무', create_date = timezone.now())
...     q.save()
...
Traceback (most recent call last):
  File "C:\venvs\mysite\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\venvs\mysite\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: pybo_question.author_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 3, in <module>
  File "C:\venvs\mysite\lib\site-packages\django\db\models\base.py", line 753, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\venvs\mysite\lib\site-packages\django\db\models\base.py", line 790, in save_base
    updated = self._save_table(
  File "C:\venvs\mysite\lib\site-packages\django\db\models\base.py", line 895, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:\venvs\mysite\lib\site-packages\django\db\models\base.py", line 933, in _do_insert
    return manager._insert(
  File "C:\venvs\mysite\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\venvs\mysite\lib\site-packages\django\db\models\query.py", line 1254, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\venvs\mysite\lib\site-packages\django\db\models\sql\compiler.py", line 1397, in execute_sql
    cursor.execute(sql, params)
  File "C:\venvs\mysite\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\venvs\mysite\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\venvs\mysite\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\venvs\mysite\lib\site-packages\django\db\backends\utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "C:\venvs\mysite\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\venvs\mysite\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\venvs\mysite\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: pybo_question.author_id ***

이런식으로 오류가 뜨는데 model.py 문제인가 싶어서 확인 해봤더니 그건 아니었어요.
models.py에서 Question을 저장한 이름도 써봤는데 저렇게 뜹니다ㅠㅠ
혹시 모르니 models.py 소스코드도 올립니다ㅠㅠ
몇 시간 동안 계속 고쳐봤는데 안됩니다ㅠㅠ
지금 서버 구동까지 다 한 상태에요..!

from django.db import models
from django.contrib.auth.models import User

class Question(models.Model):
    subject = models.CharField(max_length=200)
    content = models.TextField()
    create_date = models.DateTimeField()
    modify_date = models.DateTimeField(null=True, blank=True)
    voter = models.ManyToManyField(User, related_name='voter_question')
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='author_question')


    def __str__(self):
        return self.subject

class Answer(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE,
                               related_name='author_answer')
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    content = models.TextField()
    create_date = models.DateTimeField()
    modify_date = models.DateTimeField(null=True, blank=True)
    voter = models.ManyToManyField(User, related_name='voter_answer')

    author = models.ForeignKey(User, on_delete=models.CASCADE)

class Comment(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField()
    create_date = models.DateTimeField()
    modify_date = models.DateTimeField(null=True, blank=True)
    question = models.ForeignKey(Question, null=True, blank=True,
                                     on_delete=models.CASCADE)
    answer = models.ForeignKey(Answer, null=True, blank=True, on_delete=models.CASCADE)

cvbdsadk259 534

M 2022년 6월 19일 10:28 오전

Question 데이터를 저장하기 위해서는 author 에도 값을 채우셔야 합니다. - 박응용님, 2022년 6월 19일 10:29 오전 추천 , 대댓글
@박응용님 author 값을 어떻게 채우라는 말씀이실까요? - cvbdsadk259님, 2022년 6월 20일 2:03 오후 추천 , 대댓글
@cvbdsadk259님 author = User.objects.get(username='...') 으로 조회한후에 대입하시면 됩니다. - 박응용님, 2022년 6월 20일 5:18 오후 추천 , 대댓글
목록으로