Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Error: typeerror: __init__() missing 1 required positional argument: 'on_delete' - How to solve it?
Next
Answered
Paul Deschamps
  • 17
Paul Deschamps
Asked: May 12, 20222022-05-12T10:15:11+00:00 2022-05-12T10:15:11+00:00In: python

Error: typeerror: __init__() missing 1 required positional argument: ‘on_delete’ – How to solve it?

  • 17

. Advertisement .

..3..

. Advertisement .

..4..

Recently, I ran some of my python code, and it gave the warning text:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
  execute_from_command_line(sys.argv)
  File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\core\management\__init__.py", line 354, in execute_from_command_line
  utility.execute()
  File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\core\management\__init__.py", line 330, in execute
  django.setup()
  File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\__init__.py", line 24, in setup
  apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\apps\registry.py", line 112, in populate
  app_config.import_models()
  File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django-2.0-py3.5.egg\django\apps\config.py", line 198, in import_models
  self.models_module = import_module(models_module_name)
  File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\importlib\__init__.py", line 126, in import_module
  return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "C:\Users\lislis\Django\mon_site\blog\models.py", line 6, in <module>
  class Article(models.Model):
  File "C:\Users\lislis\Django\mon_site\blog\models.py", line 16, in Article
  categorie = models.ForeignKey('Categorie')
 TypeError: __init__() missing 1 required positional argument: 'on_delete'

While searching, I realized that some people added some command lines in my sample above. But I don’t think it is the best way to correct the problem – typeerror: __init__() missing 1 required positional argument: ‘on_delete’ How would you explain this trouble? or Is there a better way? Below is the detail of the command that I used:

class Article(models.Model):
  titre=models.CharField(max_length=100)
  auteur=models.CharField(max_length=42)
  contenu=models.TextField(null=True)
  date=models.DateTimeField(
  auto_now_add=True,
  auto_now=False,
  verbose_name="Date de parution"
  )
 
  def __str__(self):
  return self.titre
from django.db import models
 
 # Create your models here.
 class Categorie(models.Model):
  nom = models.CharField(max_length=30)
 
  def __str__(self):
  return self.nom
 
 
 class Article(models.Model):
  titre=models.CharField(max_length=100)
  auteur=models.CharField(max_length=42)
  contenu=models.TextField(null=True)
  date=models.DateTimeField(
  auto_now_add=True,
  auto_now=False,
  verbose_name="Date de parution"
  )
  categorie = models.ForeignKey('Categorie')
 
  def __str__(self):
  return self.titre
typeerror
  • 2 2 Answers
  • 118 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dhtutoria Expert
    2022-06-02T00:56:54+00:00Added an answer on June 2, 2022 at 12:56 am

    The cause: This error happens because on delete became a necessary argument in Django 2.0, which was released after version 1.9. The default is CASCADE in previous versions.

    The solution: To resolve this error,  the property  categorie of the class Article should be change as the following:

    categorie = models.ForeignKey('Categorie', on_delete=models.CASCADE)
    • 24
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Sacha Berthier
    2022-05-26T03:57:19+00:00Added an answer on May 26, 2022 at 3:57 am

    Django 2.x requires on_delete.

    Django Documentation

    Deprecated since version 1.9: on_delete will become a required argument in Django 2.0. In older versions it defaults to CASCADE.

    • 20
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.