如何为具有多个Genereic外键的模型创建工厂?

时间:2019-02-18 16:01:42

标签: python django factory-boy

我正在尝试为我的模型建立一个具有多个通用外键的工厂。我不能为一个以上的GFK工作。

class ContactFactory(factory.django.DjangoModelFactory):
    class Meta:
        abstract = True
        # model = ContactData
        exclude = ['content_object']
    object_id = factory.SelfAttribute('content_object.id')
    content_type = factory.LazyAttribute(
        lambda o: ContentType.objects.get_for_model(o.content_object))
    email = factory.Faker('email')
    fax_number = factory.Faker('phone_number')
    phone_number = factory.Faker('phone_number')
    mobile_number = factory.Faker('phone_number')
    external_profiles = factory.Dict({
        'xing': factory.Faker('uri'),
        'website': factory.Faker('uri'),
        'facebook': factory.Faker('uri'),
        'linkedin': factory.Faker('uri')
    })
class CandidateFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Candidate
    # id = uuid.uuid4()
    object_id = factory.Sequence(int)
    created_at = FuzzyDateTime(datetime.datetime(2008, 1, 1, tzinfo=pytz.UTC))
    updated_at = FuzzyDateTime(datetime.datetime(2008, 1, 1, tzinfo=pytz.UTC))
class CandidateContactFactory(factory.django.DjangoModelFactory):
    content_object = factory.SubFactory(CandidateFactory)

    class Meta:
        model = ContactData

我试图创建一个可以通过以下方式完成的联系人工厂

b = CandidateContactFactory() 

但是我有另一个address模型的通用外键。我该如何与工厂男孩在一起?

0 个答案:

没有答案