[英]Missing fields after migration in Django project
我有興趣學習 python 和 Django,我已經開始學習這門課程。 我在遷移項目中的模型時遇到困難。 按照課程,在“模型”一章中。
我已按照指示在 boards/models.py 文件中插入給定代碼。 代碼:
from django.db import models
from django.contrib.auth.models import User
class Board(models.Model):
name = models.CharField(max_length=30, unique=True)
description = models.CharField(max_length=100)
class Topic(models.Model):
subject = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
board = models.ForeignKey(Board, related_name='topics')
starter = models.ForeignKey(User, related_name='topics')
class Post(models.Model):
message = models.TextField(max_length=4000)
topic = models.ForeignKey(Topic, related_name='posts')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(null=True)
created_by = models.ForeignKey(User, related_name='posts')
updated_by = models.ForeignKey(User, null=True, related_name='+')`
按照“遷移模型”一章的說明並運行此命令:“python manage.py makemigrations” 我應該會收到以下消息:
“boards”的遷移:boards/migrations/0001_initial.py - 創建 model Board - 創建 model Post - 創建 model Topic - 添加字段 topic 到 post - 添加字段 updated_by 到 post
相反,我收到以下錯誤:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "D:\Desktop\myproject\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "D:\Desktop\myproject\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "D:\Desktop\myproject\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "D:\Desktop\myproject\venv\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "D:\Desktop\myproject\venv\lib\site-packages\django\apps\config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Zinkov\AppData\Local\Programs\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:\Desktop\myproject\myproject\boards\models.py", line 8, in <module>
class Topic(models.Model):
File "D:\Desktop\myproject\myproject\boards\models.py", line 11, in Topic
board = models.ForeignKey(Board, related_name='topics')
TypeError: __init__() missing 1 required positional argument: 'on_delete'`
我在以下幾行中添加了缺少的參數“on_delete”:
board = models.ForeignKey(Board, related_name='topics', on_delete=models.CASCADE)
starter = models.ForeignKey(User, related_name='topics', on_delete=models.CASCADE)
topic = models.ForeignKey(Topic, related_name='posts',max_length=256, on_delete=models.CASCADE)
created_by = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, null=True, related_name='+',max_length=256, on_delete=models.CASCADE)`
再次運行“makemigrations”命令后,遷移成功,但沒有按預期添加字段。 我的 output 是:
“boards”的遷移:boards\migrations\0001_initial.py - 創建 model Board - 創建 model Topic - 創建 model Post
代替:
“boards”的遷移:boards\migrations\0001_initial.py - 創建 model Board - 創建 model Topic - 創建 model Post - 添加字段 topic 到 post - 添加字段 updated_by 到 post
很抱歉發了這么長的帖子。 謝謝!
在使用 ForeignKey 關系創建 model 時,它接受 on_delete 參數,但該參數丟失了。 添加它應該工作。 請參考以下代碼
class Topic(models.Model):
subject = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
board = models.ForeignKey(Board, related_name='topics', on_delete=models.SET_NULL)
starter = models.ForeignKey(User, related_name='topics', on_delete=models.SET_NULL)
您可以參考此鏈接了解更多 on_delete arguments [https://www.geeksforgeeks.org/python-relational-fields-in-django-models/]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.