Code
1 #encoding =utf-8 2 from django.db import Models 3 # Create your models here. 4 class BookInfo (models. Model): #创建书本信息类, inherit models. Model 5 Booktitle=models. Charfield (max_length=20) 6 bookdata=models. Datefield () 7 class Heroinfo (models. Model): #创建英雄信息类 8 heroname=models. Charfield (max_length=10) 9 herosex=models. Booleanfield () herocontent=models. Charfield (max_length=50) herobook=models. ForeignKey (' BookInfo ') #引用外键, which is the BookInfo object
Rx:
Workaround:
Change the code on line 11th to:
Herobook=models. ForeignKey (' BookInfo ', on_delete=models. CASCADE,)
That is, add on_delete=models after the foreign key value. CASCADE
Reason:
After django2.0, define the foreign key and a one-to-one relationship when you need to add on_delete option, this parameter in order to avoid the two table data inconsistency problem, otherwise will be error:
TypeError: __init__ () Missing 1 required positional argument: ' On_delete '
To illustrate:
User=models. Onetoonefield (User)
Owner=models. ForeignKey (UserProfile)
Need to change to:
User=models. Onetoonefield (user,on_delete=models. CASCADE)--in the old version this parameter (models. CASCADE) is the default value
Owner=models. ForeignKey (userprofile,on_delete=models. CASCADE)--in the old version this parameter (models. CASCADE) is the default value
Parameter description:
On_delete has cascade, PROTECT, Set_null, Set_default, SET () five selectable values
CASCADE: This value is set to cascade Delete.
PROTECT: This value is set to report integrity errors.
Set_null: This value is set to set the foreign key to NULL, provided that NULL is allowed.
Set_default: This value is set to the default value of the foreign key.
Set (): This value setting, which invokes the outside value, can be a function.
In general, the use of cascade is possible.
Django builds database tables based on models Times __init__ () Missing 1 required positional argument: ' On_delete '