python - Django 类型错误 : id() takes exactly one argument (0 given)

标签 python mysql django

所以我一直在尝试实现一种将多个图像上传到帖子的方法。我这样做的方法是有 table 。一张用于实际帖子,一张用于上传的多张图片。我打算将它们与外键链接起来,但它不起作用。我的终端开始抛出错误“TypeError:id() 仅需要一个参数(给定 0)”。每当我迁移它时,它都会抛出此错误。

我不知道如何解决这个问题。

我的代码:

模型.py

from django.db import models
from django.utils import timezone
from django.forms import ModelForm
from django.utils.text import slugify
from django.utils.crypto import get_random_string
from django.conf import settings

from PIL import Image

import os

DEFAULT_IMAGE_ID = 1

# Create your models here.
class Projects(models.Model):
    title = models.CharField(max_length=30)
    description = models.TextField(max_length=150)
    publish_date = models.DateTimeField(auto_now=False, auto_now_add=True)
    update_date = models.DateTimeField(auto_now=True, auto_now_add=False)
    slug = models.SlugField(unique=True)
    files = models.FileField(upload_to='files/', blank=True)
    images = models.ImageField(upload_to='images/', height_field = 'img_height', width_field = 'img_width',blank=True)
    img_height = models.PositiveIntegerField(default=600)
    img_width = models.PositiveIntegerField(default=300)
    #feature_images = models.ForeignKey(P_Images, on_delete=models.CASCADE, default=DEFAULT_IMAGE_ID)
    feature_images = models.AutoField(primary_key=True)

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        # Generates a random string 
        unique_string = get_random_string(length=32)

        # Combines title and unique string to slugify
        slugtext = self.title + "-" + "unique_id=-" + unique_string
        self.slug = slugify(slugtext)

        return super(Projects, self).save(*args, **kwargs)

class P_Images(models.Model):
    p_file = models.ImageField(upload_to='images/', blank=None)
    p_uploaded_at = models.DateTimeField(auto_now_add=True, auto_now=False)
    #fk_post = models
    fk_post = models.ForeignKey(Projects, on_delete=models.CASCADE)

错误日志

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, projects, sessions
Running migrations:
  Applying projects.0005_auto_20180823_0553...Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/migrations/migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 525, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 630, in _alter_field
    new_default = self.effective_default(new_field)
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 218, in effective_default
    default = field.get_default()
  File "/home/erichardson/env01/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 775, in get_default
    return self._get_default()
TypeError: id() takes exactly one argument (0 given)

我为此使用 MySQL 数据库。在我更新表以便能够相互链接后,此错误开始出现。我计划 P_Images 的 fk_post 包含外键 Projects 的 feature_image 值。

005_migration.py

import builtins
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        ('projects', '0004_auto_20180823_0547'),
    ]

    operations = [
        migrations.AlterField(
            model_name='p_images',
            name='fk_post',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='projects.Projects'),
        ),
        migrations.AlterField(
            model_name='projects',
            name='feature_images',
            field=models.IntegerField(default=builtins.id),
        ),
    ]

请告诉我迁移.py 是否告诉您一些信息或什么都没有。

最佳答案

您的迁移过程中有一些非常奇怪的事情:

models.IntegerField(default=builtins.id)

这是指内置的id函数,它需要一个参数,因为它返回Python中对象的内部ID。它与数据库 ID 无关,根本不属于这里。我只能猜测您在创建迁移时被要求提供默认值,而您只需输入 id

您应该从迁移中删除该默认值,但这可能会导致迁移无法执行。您还可以尝试默认值 0,这很有意义;但您的实际模型代码显示该字段作为主键,因此您可能有另一个后续迁移再次更改该字段;并且 0 不能用作 pk。

如果您仍处于开发阶段并且没有任何需要保留的数据,我建议完全删除您的数据库和迁移,然后使用 makemigrations 重新开始。

关于python - Django 类型错误 : id() takes exactly one argument (0 given),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51979312/

相关文章:

django - 如何在 Django-Rest-Framework 中更新 ManyToMany "self"

python - 对多个文件运行 Python 单元测试

Python 和 BeautifulSoup 编码问题

python - 向 django 用户添加个人资料图片

php - 存储 PHP 应用程序设置的最佳方式?

mysql - 在node,js中等待MySQL查询执行

python - 一个没有名字的命令,在 Click

php - 根据选定的 ID 通过 PHP 重定向用户

python - Django:强制一个字段对于具有相同外键的所有模型对象都是唯一的

python - Django:向模型类对象添加附加属性