0
我反彈到這個錯誤。 我Models.py文件NOT NULL約束失敗:news_comment.post_id
class Post(models.Model):
user = models.ForeignKey(User,related_name='posts')
created_at = models.DateTimeField(auto_now=True)
message = models.TextField()
message_html = models.TextField(editable=False)
def __str__(self):
return self.message
def save(self,*args,**kwargs):
self.message_html = misaka.html(self.message)
super().save(*args,**kwargs)
def get_absolute_url(self):
return reverse('news:single',kwargs={'username':self.user.username,'pk':self.pk})
class Meta():
ordering = ['-created_at']
class Comment(models.Model):
post = models.ForeignKey('news.Post',related_name='comments')
aurthor = models.CharField(blank=False, max_length=100)
comment = models.TextField(blank=True)
created_date = models.DateTimeField(auto_now = True)
comment_html = models.TextField(editable = False)
def save(self,*args,**kwargs):
self.comment_html = misaka.html(self.comment)
super().save(*args,**kwargs)
def get_absolute_url(self):
return reverse('news:single',kwargs={'username':self.user.username,'pk':self.pk})
def __str__(self):
return self.comment
和我Views.py文件
class CommentCreateView(LoginRequiredMixin,generic.CreateView):
model = models.Comment
fields = ('comment',)
login_url = "https://stackoverflow.com/users/login"
def form_valid(self,form,*args,**kwargs):
self.object = form.save(commit = False)
self.object.aurthor = self.request.user
#self.object.post_id = self.kwargs['pk']
#print(self.request,self.kwargs['pk'])
self.object.save()
return super().form_valid(form)
而我得到的錯誤是
IntegrityError at /posts/4/comment/
NOT NULL constraint failed: news_comment.post_id
Request Method: POST
Request URL: http://localhost:8000/posts/4/comment/
Django Version: 1.11.3
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: news_comment.post_id
Exception Location: C:\Users\Sahil\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 328
Python Executable: C:\Users\Sahil\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.0
Python Path:
['C:\\Users\\Sahil\\Documents\\GitHub\\news-for-good\\my_app',
'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32',
'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages']
Server time: Tue, 25 Jul 2017 16:21:26 +0000
誰能請告訴錯誤是什麼。
如果您需要的代碼的其餘部分提前This My Github Repo
謝謝... 謝謝:)