1

OneToOne 関係を持つ Django モデル用の多数のファクトリをセットアップしようとしていますが、それらは外部キーと同じように動作しないようです。

unittest を実行すると、メイン モデルに関係が設定されていません。

私のモデル:

class ThePlan(models.Model):
    user = models.ForeignKey("User")
    creationdate = models.DateField(auto_now_add=True)


class OldPlan(models.Model):
    plan = models.OneToOneField("ThePlan")
    theplan = CharField(max_length  = 200,)


class NewPlan(models.Model):
    plan = models.OneToOneField("ThePlan")
    theplan = CharField(max_length  = 200,)

私の工場:

class ThePlanFactory(factory.DjangoModelFactory):
    FACTORY_FOR = "mysite.PlanModel"

    user = factory.SubFactory(UserFactory)


class OldPlanFactory(factory.DjangoModelFactory):
    FACTORY_FOR = "mysite.OldModel"

    plan = factory.RelatedFactory(ThePlanFactory)
    theplan = ''


class NewPlanFactory(factory.DjangoModelFactory):
    FACTORY_FOR = "mysite.NewModel"

    plan = factory.RelatedFactory(ThePlanFactory)
    theplan = ''

私のテストsetUp()では、次のことを行っています。

def setUp(self):
    self.user = factories.UserFactory.create()

    self.plan = factories.ThePlanFactory.create(
        user=self.user
    )
    self.oldplan = factories.OldPlanFactory.create(
        plan=self.plan
    )
    self.newplan = factories.NewPlanFactory.create(
        plan=self.plan
    )

したがって、これを含めてテストを実行すると、DoesNotExist: ThePlan has no OldPlan.

ここでどこが間違っていますか?私がcreateすぐに呼び出している問題はありますか?代わりに、で工場をセットアップしbuild、関係を設定してから、save

4

1 に答える 1