uranusjr / django-tutorial-for-programmers

Chapter text and example code for the Django tutorial first appeared during ITHome Ironman 7
289 stars 93 forks source link

update the ForeignKey field for Django 2.0 #42

Open christinesfkao opened 6 years ago

christinesfkao commented 6 years ago

ForeignKey is a Django field for defining a many-to-one relationship.

Up until Django 1.9 the ForeignKey field required a single argument: the model to map to. Since Django 2.0 the ForeignKey field requires two positional arguments:

If without the second argument, error message shows:

File "~/lunch/lunch/stores/models.py", line 16, in MenuItem
    store = models.ForeignKey('Store', related_name='menu_items')
TypeError: __init__() missing 1 required positional argument: 'on_delete'

You can find more about on_delete by reading the documentation.

Reference: https://www.valentinog.com/blog/django-missing-argument-on-delete/