0

我正在使用model_mommyDjango 创建测试对象。我想实现Recipe功能。我有一个模型Teacher和一个模型TeacherSchedule

Teacher(models.Model):
   some fields ...


TeacherSchedule(models.Model):
   teacher = models.OneToOneField(
        'Teacher',
        on_delete=models.CASCADE,
        related_name='schedule',
    )
   some fields...

我尝试使用的食谱:

schedule = Recipe(
    TeacherSchedule,
)

teacher_with_schedule = Recipe(
    Teacher,
    schedule=foreign_key('schedule'),
)

但是,当我运行测试时,似乎TeacherSchedule没有创建对象。我做错了Recipe什么吗?

4

1 回答 1

0

您需要删除''from 'schedule'

schedule = Recipe(
    TeacherSchedule,
)

teacher_with_schedule = Recipe(
    Teacher,
    schedule=foreign_key(schedule),
)

另外,建议您使用 Model Mommy 的更新版本,现在称为Model Bakery

于 2020-03-04T15:17:56.160 回答