在Django做makemigrations的时候,你可能遇到如题所示的错误,具体如下:

PS E:\PyCharm_data\Django_Me_G7AI> python .\manage.py makemigrations
Traceback (most recent call last):
  File ".\manage.py", line 21, in 
    main()
  File ".\manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\adamhuan\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\adamhuan\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\adamhuan\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\adamhuan\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\adamhuan\AppData\Local\Programs\Python\Python37\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\adamhuan\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 1006, in _gcd_import
  File "", line 983, in _find_and_load
  File "", line 967, in _find_and_load_unlocked
  File "", line 677, in _load_unlocked
  File "", line 728, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "E:\PyCharm_data\Django_Me_G7AI\MicroBlog\models.py", line 8, in 
    class MicroBlogPost(models.Model):
  File "E:\PyCharm_data\Django_Me_G7AI\MicroBlog\models.py", line 11, in MicroBlogPost
    author = models.ForeignKey(User,related_name="microblog_post",on_delete=models.CASCADE())
TypeError: CASCADE() missing 4 required positional arguments: 'collector', 'field', 'sub_objs', and 'using'
PS E:\PyCharm_data\Django_Me_G7AI>

发生该问题是因为,在Django的应用的数据模块类中,我的类属性定义用了外键。

发生错误的时候,我的代码如下:

文件:models.py

from django.db import models

# Create your models here.

from django.utils import timezone
from django.contrib.auth.models import User

class MicroBlogPost(models.Model):

    post_id = models.IntegerField()
    author = models.ForeignKey(User,related_name="microblog_post",on_delete=models.CASCADE())
    body = models.TextField()
    createtime = models.DateTimeField(default=timezone.now())

    class Meta:
        ordering = ("-createtime",)

    def __str__(self):
        return self.post_id

注意看其中:

author = models.ForeignKey(User,related_name="microblog_post",on_delete=models.CASCADE())

这一部分写的有问题。

需要改成这样:

author = models.ForeignKey(User,related_name="microblog_post",on_delete=models.CASCADE)

然后,再次【makemigrations】就没问题了:

PS E:\PyCharm_data\Django_Me_G7AI> python .\manage.py makemigrations
System check identified some issues:

WARNINGS:
MicroBlog.MicroBlogPost.createtime: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Migrations for 'MicroBlog':
  MicroBlog\migrations\0001_initial.py
    - Create model MicroBlogPost
PS E:\PyCharm_data\Django_Me_G7AI>

——————————
Done。

说点什么

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒