Django says "id cannot be NULL", but why?

Today I'm going crazy. I just tried to insert a new entry and it threw the error "post_blogpost.id cannot be NULL". Here is my model:

class BlogPost(models.Model):
    title   = models.CharField(max_length=100)
    slug    = models.SlugField(max_length=100)
    who     = models.ForeignKey(User, default=1)
    when    = models.DateTimeField()

    intro   = models.TextField(blank=True, null=True)
    content = models.TextField(blank=True, null=True)

    counter = models.PositiveIntegerField(default=0)

    published = models.BooleanField(default=False)
    css = models.TextField(blank=True, null=True)

    class Meta:
        ordering = ('-when', 'id')

There are many functions under the model, but I will not include them here in full. Their names are: content_cache_key, clear_cache, __unicode__, reads, read, processed_content.

I add through the administrator ... And my hair is running out.

+3
source share
2 answers

The only thing I can think of is that the table schema was out of sync with the model as someone removed the attribute AUTOINCREMENTfrom the PK of the table.

+6

admin. . , , , - , . , .

, .

$ python manage.py shell
>>> from models import *
>>> b = BlogPost(title='Hello', slug='hello')
>>> b.save()
+1

Source: https://habr.com/ru/post/1738984/


All Articles