Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

python - Factoryboy returns wrong field when using subfactory

I have a table FilmWork which is linked to a table Perosn via FilmWorkPerson table. I am trying to populate them with fake data

I have basically used this example: https://factoryboy.readthedocs.io/en/rbarrois-guide/recipes.html#many-to-many-relation-with-a-through

factories.py:

  class PersonTestFactory(DjangoModelFactory):
    class Meta:
        model = Person

    id = factory.Faker('uuid4')
    name = factory.Faker("sentence", nb_words=1)

 class FilmWorkTestFactory(DjangoModelFactory):
    class Meta:
        model = FilmWork

    id = factory.Faker('uuid4')
    title = factory.Faker("company")

 class FilmWorkPersoTestFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = FilmWorkPerson

    movie_id = factory.SubFactory(FilmWorkTestFactory)
    person_id = factory.SubFactory(PersonTestFactory)

class FilmWorkPersoTestFactory2(FilmWorkTestFactory):
    membership = factory.RelatedFactory(
        FilmWorkPersoTestFactory,
        factory_related_name='movie_id',
    )

But when I call FilmWorkPersoTestFactory2() , it returns an error: ['“Fisher, Smith and Ford” is not a valid UUID.']. I think it takes title instead of id and I do not understand how to point it to the correct field.

question from:https://stackoverflow.com/questions/65891878/factoryboy-returns-wrong-field-when-using-subfactory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...