Django 数据库创建或更新错误

标签 django python-3.x postgresql django-models

所以 pcaps 表的定义是:

class Pcaps(models.Model):
    uuid = models.CharField(max_length=50, unique=True)
    filename = models.CharField(max_length=200, default='')
    datetime = models.DateTimeField(default=datetime.now, blank=True)
    filehash = models.ForeignKey(Malwares)
    systemuuid = models.ForeignKey(ClonedSystem)

恶意软件表的定义是:

class Malwares(models.Model):
    name = models.CharField(max_length=100)
    filehash = models.CharField(max_length=100, unique=True)

View 文件中的代码是:

    Pcaps.objects.update_or_create(filename=pcapname, filehash=filehash, systemuuid=uuid)

恶意软件值实例化于:

    Malwares.objects.update_or_create(name=name, filehash=malwarehash)

我得到的错误是:

invalid literal for int() with base 10: 'ed01ebfbc9eb5bbea545af4d01bf5f1071661840480439c6e5babe8e080e41aa'

(ed01 ... 是 filehash 值)

我在这里犯了什么错误?

最佳答案

问题是您将 filehash 值参数设置为散列字符串,而 filehashForeignKey 字段,默认为整数。

您可能打算先创建Malwares 记录:

malware = Malwares.objects.create(name="name", filehash=malwarehash)
Pcaps.objects.update_or_create(filename=pcapname, filehash=malware, systemuuid=uuid)

关于Django 数据库创建或更新错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48129529/

相关文章:

python - 在 python 中别名 str.join

python - 为什么 find_packages 行为取决于 pip 包的导入?

python - raw_id_fields 不显示带有放大镜按钮的所有字段

python - 是否有可靠的 python 库用于获取 BibTex 条目并将其输出为特定格式?

python - 如何使用 google-app-engine-django 验证包含 App Engine 上文件的 Django 表单?

python - Macro VS Micro VS Weighted VS Samples F1 分数

python - 同步本地和 Elastic Beanstalk 数据库?

postgresql - Postgres : strange behavior with optimistic locking

postgresql - 按模式分隔 Postgres WAL?

postgresql - 如何从另一个脚本运行 postgresql 脚本?