【问题标题】:How do I fix the makemigrations command error?如何修复 makemigrations 命令错误?
【发布时间】:2021-04-19 06:09:12
【问题描述】:

我正在使用 python 3.8 和 django 3.1.7

起初我的 models.py 是这样的:

from django.db import models

class Topic(models.Model):
    """A topic the user is learning about"""
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        """Return a string representation of the model."""
        return self.text

当我发布“python manage.py makemigrations learning_logs”时,它运行良好。

但是当我将代码更改为:

from django.db import models

class Topic(models.Model):
    """A topic the user is learning about"""
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        """Return a string representation of the model."""
        return self.text


class Entry(models.Model):
    """Something specific learned about a topic"""
    topic = models.ForeignKey(Topic)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'entries'

    def __str__(self):
        """Return a string representation of the model."""
        return self.text[:50] + "..."

它给了我这个错误:

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 "/home/okab/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/okab/.local/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/home/okab/.local/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/okab/.local/lib/python3.8/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/okab/.local/lib/python3.8/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/okab/Desktop/Test/Projects/learning_log/learning_logs/models.py", line 14, in <module>
    class Entry(models.Model):
  File "/home/okab/Desktop/Test/Projects/learning_log/learning_logs/models.py", line 16, in Entry
    topic = models.ForeignKey(Topic)
TypeError: __init__() missing 1 required positional argument: 'on_delete'

你能帮我解决一下吗,因为我已经看过了。而且我认为每个错误都有自己的解决方案。

【问题讨论】:

标签: python-3.x django


【解决方案1】:

在你的模型中,Entry 模型中,将主题行更改为此

topic = models.ForeignKey(Topic, on_delete=models.CASCADE)

【讨论】:

    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 2021-03-24
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 2017-01-13
    相关资源
    最近更新 更多