简体   繁体   English

一对一的妈妈食谱

[英]mommy Recipe with OneToOne

I am using model_mommy with Django to create test objects.我在 Django 中使用model_mommy来创建测试对象。 I want to implement Recipe functionality.我想实现Recipe功能。 I have a model Teacher and a model TeacherSchedule :我有一个模型Teacher和一个模型TeacherSchedule

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


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

The Recipe I try to use then:我当时尝试使用的食谱:

schedule = Recipe(
    TeacherSchedule,
)

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

However, when I run my tests, it seems that the TeacherSchedule object is not created.但是,当我运行测试时,似乎没有创建TeacherSchedule对象。 Am I doing something wrong with the Recipe ?我在Recipe做错了吗?

You need to remove the '' from 'schedule' :您需要删除的'''schedule'

schedule = Recipe(
    TeacherSchedule,
)

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

Also, it is recommended that you use the updated version of Model Mommy, now known as Model Bakery .此外,建议您使用 Model Mommy 的更新版本,现在称为Model Bakery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM