factory.subfactory.subfactory code example

Example: factory subfactory

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = models.User

    name = "John"
    lang = factory.SelfAttribute('country.lang')
    country = factory.SubFactory(CountryFactory)

class CompanyFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = models.Company

    name = "ACME, Inc."
    country = factory.SubFactory(CountryFactory)
    # use ".." to go a step back and access to CompanyFactory attrs
    owner = factory.SubFactory(UserFactory, country=factory.SelfAttribute('..country'))