获取TypeError:__init __()缺少1个需要的位置参数:'on_delete'当试图添加父表后面的子表

解决办法:https://stackoverflow.com/questions/44026548/getting-typeerror-init-missing-1-required-positional-argument-on-delete

 

 1 class Article(models.Model):
 2     titre=models.CharField(max_length=100)
 3     auteur=models.CharField(max_length=42)
 4     contenu=models.TextField(null=True)
 5     date=models.DateTimeField(
 6         auto_now_add=True,
 7         auto_now=False,
 8         verbose_name="Date de parution"
 9     )
10 
11     def __str__(self):
12         return self.titre
13 我添加了父表后,现在我models.py看起来像这样:
14 
15 from django.db import models
16 
17 # Create your models here.
18 class Categorie(models.Model):
19     nom = models.CharField(max_length=30)
20 
21     def __str__(self):
22         return self.nom
23 
24 
25 class Article(models.Model):
26     titre=models.CharField(max_length=100)
27     auteur=models.CharField(max_length=42)
28     contenu=models.TextField(null=True)
29     date=models.DateTimeField(
30         auto_now_add=True,
31         auto_now=False,
32         verbose_name="Date de parution"
33     )
34     categorie = models.ForeignKey('Categorie')
35 
36     def __str__(self):
37         return self.titre
38 所以当我运行时python manage.py makemigrations <my_app_name>,我得到这个错误:
39 
40 Traceback (most recent call last):
41   File "manage.py", line 15, in <module>
42     execute_from_command_line(sys.argv)
43   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
44     utility.execute()
45   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
46     django.setup()
47   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
48     apps.populate(settings.INSTALLED_APPS)
49   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
50     app_config.import_models()
51   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
52     self.models_module = import_module(models_module_name)
53   File "C:\Users\lislis\AppData\Local\Programs\Python\Python35-32\lib\importlib\__init__.py", line 126, in import_module
54     return _bootstrap._gcd_import(name[level:], package, level)
55   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
56   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
57   File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
58   File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
59   File "<frozen importlib._bootstrap_external>", line 665, in exec_module
60   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
61   File "C:\Users\lislis\Django\mon_site\blog\models.py", line 6, in <module>
62     class Article(models.Model):
63   File "C:\Users\lislis\Django\mon_site\blog\models.py", line 16, in Article
64     categorie = models.ForeignKey('Categorie')
65 TypeError: __init__() missing 1 required positional argument: 'on_delete'

 

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

我添加了父表后,现在我models.py看起来像这样:

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

所以当我运行时python manage.py makemigrations <my_app_name>,我得到这个错误:

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'

posted on 2018-03-09 13:24  NothingLZ  阅读(450)  评论(0编辑  收藏  举报

导航