AttributeError: module 'django.db.models' has no attribute 'RESTRICT'

Viewed 948

Both CASCASE and PROTECT are working, but I cannot get RESTRICT to work. I am going through the Mozilla tutorial.

book = models.ForeignKey('Book', on_delete=models.RESTRICT, null=True)

I am getting this error when I try to make migrations

AttributeError: module 'django.db.models' has no attribute 'RESTRICT'

I am running django 3.2.4

3 Answers

it is the version compatibility problem

I faced the same problem for django 3.2.4

 "AttributeError: module 'django.db.models' has no attribute 'RESTRICT'"

Solution

-> downgrade django to 3.1.2

   pip install django==3.1.2  (if you are using python environment)

-> it works perfectly now. I hope this works for you

make sure you are importing models like below

from django.db import models

#remove null=True from your model field definition

You need to activate your virtual environment before run your command.

$ python3 manage.py makemigrations

you can activate your virtual environment just run following command on your terminal where you have installed virtual environment and created your virtual environment.

$ . your_env_name/bin/activate

If you don't have any virtual environment , install and create a virtual environment before run any python command for django.

Related