获取 TypeError: __init__() 缺少 1 个必需的位置参数:'on_delete' 尝试在带有条目的子表之后添加父表时 [英] Getting TypeError: __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries

查看:25
本文介绍了获取 TypeError: __init__() 缺少 1 个必需的位置参数:'on_delete' 尝试在带有条目的子表之后添加父表时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 sqlite 数据库中有两个类,一个名为 Categorie 的父表和名为 Article 的子表.我首先创建了子表类并添加了条目.所以首先我有这个:

I have two classes in my sqlite database, a parent table named Categorie and the child table called Article. I created first the child table class and addes entries. So first I had this:

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 看起来像这样:

And after I have added parent table, and now my models.py looks like this:

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> 时,我得到这个错误:

So when I run python manage.py makemigrations <my_app_name>, I get this error:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:UserslislisAppDataLocalProgramsPythonPython35-32libsite-packagesdjango-2.0-py3.5.eggdjangocoremanagement\__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "C:UserslislisAppDataLocalProgramsPythonPython35-32libsite-packagesdjango-2.0-py3.5.eggdjangocoremanagement\__init__.py", line 330, in execute
    django.setup()
  File "C:UserslislisAppDataLocalProgramsPythonPython35-32libsite-packagesdjango-2.0-py3.5.eggdjango\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:UserslislisAppDataLocalProgramsPythonPython35-32libsite-packagesdjango-2.0-py3.5.eggdjangoapps
egistry.py", line 112, in populate
    app_config.import_models()
  File "C:UserslislisAppDataLocalProgramsPythonPython35-32libsite-packagesdjango-2.0-py3.5.eggdjangoappsconfig.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:UserslislisAppDataLocalProgramsPythonPython35-32libimportlib\__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:UserslislisDjangomon_sitelogmodels.py", line 6, in <module>
    class Article(models.Model):
  File "C:UserslislisDjangomon_sitelogmodels.py", line 16, in Article
    categorie = models.ForeignKey('Categorie')
TypeError: __init__() missing 1 required positional argument: 'on_delete'

我在stackoverflow中看到了一些类似的问题,但似乎不是同一个问题:__init__() 缺少 1 个必需的位置参数:'quantity'

I've seen some similar issues in stackoverflow, but it seems to not be the same problem: __init__() missing 1 required positional argument: 'quantity'

推荐答案

你可以像这样改变Article类的属性categorie:

You can change the property categorie of the class Article like this:

categorie = models.ForeignKey(
    'Categorie',
    on_delete=models.CASCADE,
)

错误应该消失.

最终您可能需要 on_delete 的另一个选项,请查看文档以获取更多详细信息:

Eventually you might need another option for on_delete, check the documentation for more details:

https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.ForeignKey

正如您在评论中所述,您对 on_delete 没有任何特殊要求,您可以使用选项 DO_NOTHING:

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,
# ...

这篇关于获取 TypeError: __init__() 缺少 1 个必需的位置参数:'on_delete' 尝试在带有条目的子表之后添加父表时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆