У меня есть 2 модели (временная шкала, которая будет содержать информацию по умолчанию, которую я должен загрузить, и PDF-версия, которая содержит файл и отношение к одной из ячеек временной шкалы). Мне сказали создать собственный файл миграции, и я выполнил следующее, но у меня появляется эта ошибка, и я не могу найти в Интернете ничего об этом:
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
Мои модели:
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)
Мой файл миграции:
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),
]