TypeError 가져 오기 : __init __ () 필수 위치 인수 1 개 누락 : 항목이있는 자식 테이블 뒤에 부모 테이블을 추가하려고 할 때 'on_delete'
내 sqlite 데이터베이스에 두 개의 클래스, 이름이 부모 테이블 Categorie
이고 자식 테이블이 Article
. 먼저 자식 테이블 클래스를 만들고 항목을 추가합니다. 그래서 먼저 이것을 가지고 있습니다.
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'
나는 stackoverflow에서 유사한 문제를 보았지만 동일한 문제가 아닌 것 같습니다 : __init __ () 1 개의 필수 위치 인수 누락 : 'quantity'
다음 과 같이 categorie
클래스 의 속성 을 변경할 수 있습니다 Article
.
categorie = models.ForeignKey(
'Categorie',
on_delete=models.CASCADE,
)
오류가 사라져야합니다.
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
EDIT:
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,
# ...
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.
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.
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)
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.
Here are available options if it helps anyone for on_delete
CASCADE, DO_NOTHING, PROTECT, SET, SET_DEFAULT, SET_NULL
This worked for me pip install django-csvimport --upgrade
'Nice programing' 카테고리의 다른 글
편집 텍스트 커서 색상 변경 (0) | 2020.10.28 |
---|---|
Html Agility Pack은 클래스별로 모든 요소를 가져옵니다. (0) | 2020.10.28 |
네비게이션 컨트롤러의 투명한 모달 뷰 (0) | 2020.10.28 |
루트에서 하위 디렉터리로 IIS7 URL 리디렉션 (0) | 2020.10.28 |
파이썬에서 신호에 잡음 추가 (0) | 2020.10.28 |