python - NOT NULL constraint failed: news_comment.post_id -


i bounced error. models.py file is

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 

and views.py file

class commentcreateview(loginrequiredmixin,generic.createview):     model = models.comment     fields = ('comment',)      login_url = "/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) 

and error is

integrityerror @ /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 

can please tell error is.

if need rest of code this github repo

thanks in advance... thank :)

you have forget add post foreignkey field save in views. because did not defined null=true in post foreignkey field in comment model. need assign post model instance when saving comment model instance

def form_valid(self,form,*args,**kwargs):         self.object = form.save(commit = false)         self.object.aurthor = self.request.user         self.object.post = # assign post model instance         self.object.save()         return super().form_valid(form) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -