229π
You can change the property categorie
of the class Article
like this:
categorie = models.ForeignKey(
'Categorie',
on_delete=models.CASCADE,
)
and the error should disappear.
Eventually you might need another option for on_delete
, check the documentation for more details:
Arguments β Model field reference β Django documentation
As you stated in your comment, that you donβt have any special requirements for on_delete
, you could use the option DO_NOTHING
:
# ...
on_delete=models.DO_NOTHING,
# ...
50π
Since Django 2.x, on_delete
is required.
Deprecated since version 1.9: on_delete will become a required
argument in Django 2.0. In older versions it defaults to CASCADE.
- [Django]-Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT
- [Django]-Django QuerySet order
- [Django]-Django urlsafe base64 decoding with decryption
17π
Since Django 2.0 the ForeignKey field requires two positional arguments:
- the model to map to
- the on_delete argument
categorie = models.ForeignKey('Categorie', on_delete=models.PROTECT)
Here are some methods can used in on_delete
- CASCADE
Cascade deletes. Django emulates the behavior of the SQL constraint ON DELETE CASCADE and also deletes the object containing the ForeignKey
- PROTECT
Prevent deletion of the referenced object by raising ProtectedError, a subclass of django.db.IntegrityError.
- DO_NOTHING
Take no action. If your database backend enforces referential integrity, this will cause an IntegrityError unless you manually add an SQL ON DELETE constraint to the database field.
you can find more about on_delete by reading the documentation.
- [Django]-How to use MySQLdb with Python and Django in OSX 10.6?
- [Django]-How can I chain Django's "in" and "iexact" queryset field lookups?
- [Django]-Celery. Decrease number of processes
14π
From Django 2.0 on_delete
is required:
user = models.OneToOneField(User, on_delete=models.CASCADE)
It will delete the child table data if the User is deleted. For more details check the Django documentation.
- [Django]-Reducing Django Memory Usage. Low hanging fruit?
- [Django]-Django AutoField with primary_key vs default pk
- [Django]-Django β Circular model import issue
4π
If you are using foreignkey then you have to use βon_delete=models.CASCADEβ as it will eliminate the complexity developed after deleting the original element from the parent table. As simple as that.
categorie = models.ForeignKey('Categorie', on_delete=models.CASCADE)
- [Django]-Django Generic Views using decorator login_required
- [Django]-What is the difference render() and redirect() in Django?
- [Django]-Django can' t load Module 'debug_toolbar': No module named 'debug_toolbar'
4π
Post Django version 1.9,
on_delete
became a required argument, i.e. from Django 2.0.
In older versions, it defaults to CASCADE.
So, if you want to replicate the functionality that you used in earlier versions. Use the following argument.
categorie = models.ForeignKey('Categorie', on_delete = models.CASCADE)
This will have the same effect as that was in earlier versions, without specifying it explicitly.
Official Documentation on other arguments that go with on_delete
- [Django]-Split views.py in several files
- [Django]-Django: remove a filter condition from a queryset
- [Django]-How to define two fields "unique" as couple
3π
Here are available options if it helps anyone for on_delete
CASCADE, DO_NOTHING, PROTECT, SET, SET_DEFAULT, SET_NULL
- [Django]-Django: Fat models and skinny controllers?
- [Django]-How do I add a placeholder on a CharField in Django?
- [Django]-Django: How to check if the user left all fields blank (or to initial values)?
3π
If you donβt know which option to enter the params.
Just want to keep the default value like on_delete=None
before migration:
on_delete=models.CASCADE
This is a code snippet in the old version:
if on_delete is None:
warnings.warn(
"on_delete will be a required arg for %s in Django 2.0. Set "
"it to models.CASCADE on models and in existing migrations "
"if you want to maintain the current default behavior. "
"See https://docs.djangoproject.com/en/%s/ref/models/fields/"
"#django.db.models.ForeignKey.on_delete" % (
self.__class__.__name__,
get_docs_version(),
),
RemovedInDjango20Warning, 2)
on_delete = CASCADE
- [Django]-How do I get user IP address in Django?
- [Django]-How to change a django QueryDict to Python Dict?
- [Django]-Update all models at once in Django
2π
"models.ForeignKey()" and "models.OneToOneField" must have "on_delete" since Django 2.0. For example below:
"models.ForeignKey()":
categories = models.ForeignKey('Category', on_delete=models.CASCADE)
"models.OneToOneField":
categories = models.OneToOneField('Category', on_delete=models.PROTECT)
- [Django]-Equivalent of PHP "echo something; exit();" with Python/Django?
- [Django]-Django admin: How to display the field marked as "editable=False" in the model?
- [Django]-How to use MySQLdb with Python and Django in OSX 10.6?
0π
Had a similar problem that resolved by adding both these two parameters to ForeignKey:
null=True, on_delete=models.SET_NULL
- [Django]-Getting Django admin url for an object
- [Django]-How to convert a Django QuerySet to a list?
- [Django]-How can I test https connections with Django as easily as I can non-https connections using 'runserver'?
0π
For me the package manager was resolving djangorestframework
to a lower version and the authtoken
model did not have the on_delete. All of my models were good. You can run django.apps.apps.get_models()
to get a list of all your models to see what libraries create their own models and might be the issue.
- [Django]-How to obtain and/or save the queryset criteria to the DB?
- [Django]-How do I do a not equal in Django queryset filtering?
- [Django]-Django database query: How to get object by id?
- [Django]-Why does my Django admin site not have styles / CSS loading?
- [Django]-ValueError: The field admin.LogEntry.user was declared with a lazy reference
- [Django]-Django removing object from ManyToMany relationship