Django builds database tables based on models Times __init__ () Missing 1 required positional argument: ' On_delete '

Source: Internet
Author: User

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 '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.