gpt4 book ai didi

python - 无法创建 Django 模型的实例

转载 作者:行者123 更新时间:2023-11-29 12:23:41 33 4
gpt4 key购买 nike

我正在尝试创建此报告模型的实例:

class Report(models.Model):
"""
A model for storing credit reports pulled from Equifax.
"""
user = models.ForeignKey(to=CustomUserModel, on_delete=models.CASCADE,
help_text='User report belongs to.')

timestamp = models.DateTimeField(default=timezone.now)
report = JSONField()

但是,每当我尝试时都会出现此错误:

Exception Type: TypeError at /internal/report
Exception Value: 'report' is an invalid keyword argument for this function

无论我是使用 Report().save() 方法还是使用 Report.object.create() 方法实例化实例,都会发生这种情况,如下所示:

    report_obj = Report.objects.create(
user=user,
report=report
)

有人知道发生了什么事吗?该类显然有一个“报告”属性,那么为什么会出现错误?

谢谢!

最佳答案

基于错误和 comment :

(...) Looks like I imported the form field from DRF instead of the model field of the same name from Django (...)

您没有导入 JSONField那是一个模型字段,但是其他东西(例如 form 字段,或者这里是 DRF 字段)。因此,Django 不会将 report 视为您的 Report 模块的字段,而是将其视为“vanilla”Python 属性。

因此,您应该确保 JSONField 链接到模型字段类。添加这样的字段可能会导致另一个迁移以将字段添加到数据库表中:

from django.contrib.postgres.fields import <b>JSONField</b>

class Report(models.Model):
"""
A model for storing credit reports pulled from Equifax.
"""
user = models.ForeignKey(to=CustomUserModel, on_delete=models.CASCADE,
help_text='User report belongs to.')

timestamp = models.DateTimeField(default=timezone.now)
report = JSONField()

关于python - 无法创建 Django 模型的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54715460/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com