IntegrityError at ** NOT NULL constraint failed: learning_logs_topic.owner_id

in chapter 19 of python from introduction to practice, models add the user attribute of topic in learning_logs, that is, the topic created by the logged user, then the owner attribute of this topic is the logged user, code: Owner = model.foreignkey (User,on_delete= model.cascade), owner is the ForeignKey of User (pthon version 3.7.4, django version 2.2.6) and is coming up after makations:

, then select the option of ‘1)’, then select the user ID of 1, namely ll_admin, then the attribution user ID of the original topic is 1, namely ll_admin. The following error occurs when executing new_topic:

IntegrityError at /new_topic/ NOT NULL constraint failed: learning_logs_top.owner_id, error site as follows:

, of which topic model source code is as follows:

class Topic(models.model) :
text= model.charfield (max_length=200)
date_added= model.datetimefield (auto_now_add=True)
owner= model.foreignkey (User,on_delete= model.cascade)
def the __str__ (self) :
return self. The text

topicform source as follows:

class TopicForm (forms. ModelForm) :
class Meta:
model = Topic
fields = [‘ text ‘]
labels = {‘ text: “‘}

: new_topic under the learning_logs folder views. Py source code is as follows:

def new_topic(request) :
if request. Method! : = ‘POST’
form = TopicForm ()

the else:
form = TopicForm (request. POST).
if the form is_valid () :
form. The save ()
the return HttpResponseRedirect(‘learning_logs:topics’)

context={‘form’:form}
return render(request,’learning_logs/ new_top.html ‘,context)

this error shows null value limitation error: owner, owner is the attribute just added in models. Although the owner attribute is added in the existing topic, the newly added topic does not have an owner attribute, so an error is reported.

is solved as follows:

(1) add null=True to the models.py attribute, allowing this attribute to be empty

becomes owner= model.foreignkey after modification (User,on_delete= model.cascade, null=True). After modification, the error does not occur, but the owner of the newly added topic is null, and

is not intended by design

(2) adds the default value inside models.py.

modified owner= model.foreignkey (User,on_delete= model.cascade, default=1), and the modified error will not occur, but the owner of newly added topic is always the User with ID 1, and it is not the desired

(3) modify TopicForm and add owner field

modified TopicForm into the following:

class TopicForm (forms. ModelForm) :
class Meta:
model = Topic
fields = [‘ text, ‘the owner’]
labels = {‘ text ‘:’, ‘owner’ : ‘}
After

is modified, the user will select the owner of the new topic, as shown below:

although the current logged in user can choose the owner of this topic, the user can also choose other non-logged in users, which increases the risk of error

(4) in the views. Py new_topic view, get the current user with request.user and assign a value to form

is to add a line after if form.is_valid() :

form.instance.owner=request.user,

def new_topic(request) :
if request. Method! : = ‘POST’
form = TopicForm ()

the else:
form = TopicForm (request. POST).
if the form is_valid () :
form. The instance. The owner = request. User
Form. The save ()
return HttpResponseRedirect (reverse (‘ learning_logs: switchable viewer ‘))

the context = {‘ form: form}
the return render(request,’learning_logs/new_topic.html’,context)

USES the instance parameter of the Form to get the owner attribute and assigns the current user obtained by requset.user to the owner of the instance. After the modification, the program realizes the predetermined function

(5) also takes advantage of the instance parameter, but at the time of the generated Form, the modified new_topic view is as follows:

def new_topic(request) :
if request. Method! : = ‘POST’
form = TopicForm ()

the else:
form = TopicForm (request. POST)
topic = topic ()
topic. The owner = request. User
form = TopicForm (request. POST, the instance = topic).
if the form is_valid () :
form. The save ()
return HttpResponseRedirect (reverse (‘ learning_logs: switchable viewer ‘))

The context = {‘ form: form}
return render (request, ‘learning_logs/new_topic. HTML, context)

becomes a topic, then assign the value to the owner, and then assign the topic to the instance parameter, which costs a lot of space and time, but also achieves the predetermined function.

Read More: