首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >模型的Django默认信息

模型的Django默认信息
EN

Stack Overflow用户
提问于 2020-03-25 04:20:58
回答 1查看 72关注 0票数 0

我有2个模型(时间线一个将包含默认信息,我必须上传,和Pdf的一个包含文件和一个关系的时间线单元格)。我被告知要创建自己的迁移文件,并执行了以下操作,但我收到此错误,并且在网上找不到任何有关它的内容:

代码语言:javascript
运行
复制
 File "/Users/fetz/Desktop/parentsuportal/parentsuportal/timeline/migrations/0005_auto_20200324_1721.py", line 33, in addData
    Timeline(header = "Transport Support", age = "18-25")
  File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 520, in __hash__
    raise TypeError("Model instances without primary key value are unhashable")
TypeError: Model instances without primary key value are unhashable

我的模型:

代码语言:javascript
运行
复制
HEADER_CHOICES = [
    ('Financial Support', 'Financial Support'),
    ('Educational Support', 'Educational Support'),
    ('Governmental Support', 'Governmental Support '),
    ('Charity Support Groups', 'Charity Support Groups'),
    ('Therapy Support', 'Therapy Support '),
    ('Transport Support', 'Transport Support ')
]
AGE_CHOICES = [
    ('0-4', '0-4'),
    ('4-11', '4-11'),
    ('11-18', '11-18'),
    ('18-25', '18-25')
]

class Timeline(models.Model):
    header = models.CharField(max_length=30, choices=HEADER_CHOICES)
    age = models.CharField(max_length=6, choices=AGE_CHOICES)

class Pdf(models.Model):
    pdf = models.FileField(upload_to='timelinepdfs')
    timeline = models.ForeignKey(Timeline, on_delete=models.CASCADE)

我的迁移文件:

代码语言:javascript
运行
复制
from django.db import migrations

def addData(apps, schema_editor):
    # We can't import the Person model directly as it may be a newer
    # version than this migration expects. We use the historical version.
    Timeline = apps.get_model("timeline", "Timeline")
    timeline = {
        Timeline(header = "Financial Support", age = "0-4"),
        Timeline(header = "Financial Support", age = "4-11"),
        Timeline(header = "Financial Support", age = "11-18"),
        Timeline(header = "Financial Support", age = "18-25"),
        Timeline(header = "Educational Support", age = "0-4"),
        Timeline(header = "Educational Support", age = "4-11"),
        Timeline(header = "Educational Support", age = "11-18"),
        Timeline(header = "Educational Support", age = "18-25"),
        Timeline(header = "Governmental Support", age = "0-4"),
        Timeline(header = "Governmental Support", age = "4-11"),
        Timeline(header = "Governmental Support", age = "11-18"),
        Timeline(header = "Governmental Support", age = "18-25"),
        Timeline(header = "Charity Support Groups", age = "0-4"),
        Timeline(header = "Charity Support Groups", age = "4-11"),
        Timeline(header = "Charity Support Groups", age = "11-18"),
        Timeline(header = "Charity Support Groups", age = "18-25"),
        Timeline(header = "Therapy Support", age = "0-4"),
        Timeline(header = "Therapy Support", age = "4-11"),
        Timeline(header = "Therapy Support", age = "11-18"),
        Timeline(header = "Therapy Support", age = "18-25"),
        Timeline(header = "Transport Support", age = "0-4"),
        Timeline(header = "Transport Support", age = "4-11"),
        Timeline(header = "Transport Support", age = "11-18"),
        Timeline(header = "Transport Support", age = "18-25")
    }
    timeline.save()

class Migration(migrations.Migration):

    dependencies = [
        ('timeline', '0004_auto_20200323_1947'),
    ]

    operations = [
         migrations.RunPython(addData),
    ]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-25 05:14:15

您需要先保存时间轴实例,然后才能在集中使用它。这是因为set使用__hash__方法来标识实例(以便集合具有惟一的项),而Django Model类的__hash__方法需要pk。解决方案是使用不同的集合类,比如list并对其进行迭代。

代码语言:javascript
运行
复制
timelines = [
    Timeline(header = "Financial Support", age = "0-4"),
    Timeline(header = "Financial Support", age = "4-11"),
    ...
]
for timeline in timelines:
    timeline.save()

或者你可以使用:

代码语言:javascript
运行
复制
Timeline.objects.create(header = "Financial Support", age = "0-4")
Timeline.objects.create(header = "Financial Support", age = "4-11")
...

或者如果你担心它们已经存在:

代码语言:javascript
运行
复制
Timeline.objects.get_or_create(header = "Financial Support", age = "0-4")
Timeline.objects.get_or_create(header = "Financial Support", age = "4-11")
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60838705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档