SoFunction
Updated on 2024-12-10

Perfect solution to Django2.0 under the models ForeignKey problem

Writing ForeignKey under models class in Django 2.0

book = ('BookInfo')

django 2.0 differs from its predecessor, 1.8.

Error:

book = ('BookInfo')
TypeError: __init__() missing 1 required positional argument: 'on_delete'

Solution:

book = ('BookInfo', on_delete=,)

Add the on_delete parameter and you're good to go!!!!

Additional knowledge:Django ForeignKey ondelete

CASCADE: Deletes all the information under the associated table;

PROTECT: When deleting information, a protection mechanism is adopted to throw an error: i.e., the contents of the associated table are not deleted;

SET_NULL: Null the association only if null=True;

SET_DEFAULT: Set to the default value;

SET( ): the parentheses can be functions, set to something you define;

DO_NOTHING: Literally, don't do anything, you're deleting what you're deleting, it doesn't matter to me.

Above this perfect solution to Django2.0 in models under the ForeignKey () problem is all I have shared with you, I hope to give you a reference, and I hope you support me more.