[SOLVED] Django: I can't figure out how to save the model


I’m just learning django. And for 2 hours I can not understand what the mistake is.

Model Blog

class Blog(models.Model):
    title = models.CharField(max_length=255, verbose_name='Заголовок')
    text = RichTextField(verbose_name='Текст')
    date = models.DateField(auto_now_add=True, verbose_name='Дата')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='Автор')
    image = models.ImageField(verbose_name='Фото', upload_to='blog')
    tags = models.CharField(max_length=500, verbose_name='Теги', help_text='через кому')
    slug = AutoSlugField(populate_from='title', null=True)

    class Meta:
        verbose_name = 'Стаття'
        verbose_name_plural = 'Статті'

    def __str__(self):
        return self.title

Model Comment

class Comment(models.Model):
    name = models.CharField(max_length=100, verbose_name='Ім\'я')
    email = models.EmailField(verbose_name='Email', max_length=255)
    text = models.CharField(max_length=500, verbose_name='Коментар')
    datetime = models.DateTimeField(auto_now_add=True)
    article = models.ForeignKey(Blog, on_delete=models.CASCADE, verbose_name='Стаття')
    parent = models.IntegerField(verbose_name='Батьківський коментар')

    class Meta:
        verbose_name = 'Коментар'
        verbose_name_plural = 'Коментарі'

    def __str__(self):
        return self.name + ', ' + self.email

Model CommentForm

class CommentForm(ModelForm):
    class Meta:
        model = Comment
        fields = ['name', 'email', 'text']
        widgets = {
            'name': TextInput(attrs={'class': 'form-control form--control'}),
            'email': TextInput(attrs={'class': 'form-control form--control'}),
            'text': Textarea(attrs={'rows': 5, 'class': 'form-control form--control'}),


def detail(request, slug):
    blog = Blog.objects.get(slug=slug)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        form.article_id = blog.pk
        form.parent = 0
        if form.is_valid():
        form = CommentForm()

    return render(request, 'blog/blog_detail.html', context={'blog': blog, 'tags': blog.tags.split(','), 'form': form})

and I keep getting an error – (1048, "Column ‘article_id’ cannot be null"). I can’t understand why, because I give it meaning.

And yet: how can I debug a variable? For example, to see what’s in the form. I used to program in php, there was a var_dump function. Is python analog?


I recommend you change it in the following way, it will be easier for you to understand, and to debug I recommend you add in the line where you want it to stop ,
and it will let you from the console see everything from that line

def post(request, slug):
    blog = Blog.objects.get(slug=slug)
    form = CommentForm(request.POST, request.FILES)
    if form.is_valid():
        form_data = form.claned_data
        comment = Comment()
        comment.article = blog
        comment.parent = 0
        comment.email = form_data.get('email')
        comment.text = form_data.get('text')
        comment.name = form_data.get('name')
    context={'blog': blog,
            'tags': blog.tags.split(','),
            'form': form}
    return render(request,'blog/blog_detail.html',context)

## for debug 
    import pdb  
    ## this for debug do not forget remove line

Answered By – Lana

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *