django.db.utils .IntegrityError: NOT NULL constraint failed: blog_ blog.author_ ID error resolution

I am learning django these days, the project is to do my own blog, and then the shell command to create a new blog when the following error

django.db.utils.IntegrityError: NOT NULL constraint failed: blog_blog.author_id

is known from the error message that the non-null constraint of the database is the problem, and it is in blog.auther_id that the problem is

and then look at my creation process, first go back to my models model, the source code is as follows

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

#  创建博客分类模型
class BlogType(models.Model):
    type_name = models.CharField(max_length=15)
    def __str__(self):
        return self.type_name

#  创建博客模型
class Blog(models.Model):
    title = models.CharField(max_length=50)
    blog_type = models.ForeignKey(BlogType, on_delete=models.DO_NOTHING)
    content = models.TextField()
    author = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    created_time = models.DateTimeField(null=True, auto_now_add=True)
    last_updated_time = models.DateTimeField(null=True, auto_now=True)
    def __str__(self):
        return "<Blog: %s>" % self.title 

can tell me the foreign key I used in the author, and the author class I didn’t define in the model, so my shell code is as follows:

> > > from django.contrib.auth.models import User
> > > User.objects.all()
< QuerySet [<User: xxz>] >
> > > blog.auther = User.objects.all()[0]
> > >

sqlite3.IntegrityError: NOT NULL constraint failed: blog_blog.author_id

and then there’s the problem, and the solution to that is very simple, is just to type


> > > blog.author_id = 1
> > >

problem solved, so now let’s talk about how the problem came to be

here in the django document is the sentence

Model. pk

Regardless of whether you define a primary key field yourself, or let Django supply one for you, each model will have a property called pk. It behaves like a normal attribute on the model, but is actually an alias for whichever attribute is the primary key field for the model. You can read and set this value, just as you would for any other attribute, and it will update the correct field in the model.

translation is

whether you define your own primary key field or let Django provide one for you, each model has a property called pk. It behaves like a normal property on the model, but is actually an alias for an alias that belongs to the model's primary key field. You can read and set this value as you would any other property, and it updates the correct fields in the model.

general meaning is that you defined in the models of all the classes (as a table in the database), there will be a primary key id, every time you create an object, like write blog = blog () above, will automatically assign an id in the blog, and the increment, by the same token, the auther in the blog, to define a id, but not defined before, so this manual are defined.

Read More: