我试图在我的models.py中使一个类与另一个类相关,但似乎只有Card类与主题正确相关。我正在尝试使它们相互关联,例如:“每个堆栈将具有多个主题,并且每个类别将具有多个卡片。在运行迁移时,我没有错误,但只说:
Migrations for 'flashcards':
flashcards/migrations/0001_initial.py
- Create model Card
- Create model Stack
- Create model Subject
- Add field subject to card
仅基于我的迁移输出,似乎只有Card与Subject有关,而Subject与Stack不相关,或者我读错了吗,它工作正常吗?我是一名新程序员,只是学习如何测试。
models.py
from django.db import models
from django.conf import settings
from django.utils import timezone
from django.contrib.auth.models import User
class Stack(models.Model): #A stack of cards that contains multiple
categories
author = models.ForeignKey(User, on_delete = models.CASCADE)
title = models.CharField(max_length = 200, unique = True)
description = models.CharField(max_length = 300)
created_date = models.DateTimeField(default = timezone.now)
published_date = models.DateTimeField(blank = True, null = True)
def __str__(self):
return self.title
class Subject(models.Model): #Subjects that are related to each stack
title = models.CharField(max_length = 200) #Title of the Category (ie:
Spanish)
description = models.CharField(max_length = 300, null = True)
#Category Description
stack = models.ForeignKey(Stack, on_delete = models.CASCADE)
class Card(models.Model): #cards that are related to each subject
subject = models.ForeignKey(Subject, on_delete = models.CASCADE)
key = models.CharField(max_length = 300) #Card Question
answer = models.CharField(max_length = 300) # Card Answer
答案 0 :(得分:1)
您似乎对运行makemigrations
所获得的输出略有误解,一切都按预期工作。
我使用您提供的代码创建了一个测试项目,并在makemigrations
应用上运行了flashcards
。查看所生成的迁移文件0001_initial.py
的内容:
# Generated by Django 2.1.7 on 2019-03-30 03:20
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel( # Summarised as: - Create model Card
name='Card',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(max_length=300)),
('answer', models.CharField(max_length=300)),
],
),
migrations.CreateModel( # Summarised as: - Create model Stack
name='Stack',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200, unique=True)),
('description', models.CharField(max_length=300)),
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
('published_date', models.DateTimeField(blank=True, null=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel( # Summarised as: - Create model Subject
name='Subject',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('description', models.CharField(max_length=300, null=True)),
# As the ``Stack`` model has been created it can include the ForeignKey definition here.
('stack', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='flashcards.Stack')),
],
),
migrations.AddField( # Summarised as: - Add field subject to card
model_name='card',
name='subject',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='flashcards.Subject'),
),
]
运行makemigrations
后看到的输出是所生成迁移文件的简要摘要。
由于Django创建模型的顺序,因此无法将subject
字段添加到Card
,因为尚未创建Subject
。因此,它将跳过subject
字段,并在创建Subject
模型之后将其添加。
我可以理解为什么这看起来有点令人困惑,没有明确的消息说类似- Add field stack to subject
之类的原因是因为在stack
步骤中添加了- Create model Subject
字段。