我正试图为一个带有GFK的模型编写一个工厂进行测试,但我似乎无法让它工作。我已经参考了文档中的常见配方,但我的模型并不完全匹配,而且我还遇到了一个错误。这是我的模型
class Artwork(models.Model):
...
region = models.ForeignKey("Region", on_delete=models.SET_NULL, null=True, blank=True)
class Region(models.Model):
# Could be either BeaconRegion or SpaceRegion
region_content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
region_object_id = models.PositiveIntegerField()
region = GenericForeignKey("region_content_type", "region_object_id")
class SpaceRegion(models.Model):
label = models.CharField(max_length=255)
regions = GenericRelation(
Region,
content_type_field="region_content_type",
object_id_field="region_object_id",
related_query_name="space_region",
)
class BeaconRegion(models.Model):
label = models.CharField(max_length=255)
regions = GenericRelation(
Region,
content_type_field="region_content_type",
object_id_field="region_object_id",
related_query_name="beacon_region",
)
本质上,一个Artwork
可以放置在两个Region
s中的一个;aSpaceRegion
或BeaconRegion
.
Factory
s
class RegionFactory(factory.django.DjangoModelFactory):
region_object_id = factory.SelfAttribute("region.id")
region_content_type = factory.LazyAttribute(
lambda o: ContentType.objects.get_for_model(o.region)
)
class Meta:
exclude = ["region"]
abstract = True
class BeaconRegionFactory(RegionFactory):
label = factory.Faker("sentence", nb_words=2)
region = factory.SubFactory(RegionFactory)
class Meta:
model = Region
class SpaceRegionFactory(RegionFactory):
label = factory.Faker("sentence", nb_words=2)
region = factory.SubFactory(RegionFactory)
class Meta:
model = Region
class ArtworkFactory(factory.django.DjangoModelFactory):
...
region = factory.SubFactory(SpaceRegionFactory)
在我的测试中,我尝试使用ArtworkFactory()
创建一个艺术品,但它错误的
AttributeError: The parameter 'region' is unknown. Evaluated attributes are {}, definitions are <DeclarationSet: {'region_object_id': <SelfAttribute('region.id', default=<class 'factory.declarations._UNSPECIFIED'>)>, 'region_content_type': <factory.declarations.LazyAttribute object at 0x1068cf430>, 'label': <factory.faker.Faker object at 0x1068cf880>}>
我在这里做错了什么?
在解析ArtworkFactory.region.region
即SpaceRegionFactory.region
时出现问题。
从你们的模型来看,似乎:
Region
是指向SpaceRegion
或BeaconRegion
的表SpaceRegion
和BeaconRegion
是简单的表,有一个助手来检索相关的Region
对象。
这些复杂关系链的第一步是编写没有工厂的代码:
>>> shire = SpaceRegion(label="Shire")
>>> shire_generic = Region(region=shire)
>>> the_ring = Artwork(region=shire_generic)
这告诉我们Region
总是在SpaceRegion
或BeaconRegion
之后创建,给出以下工厂:
class SpaceRegionFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.SpaceRegion
label = factory.Faker("sentence", n_words=2)
class RegionFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Region
region = factory.SubFactory(SpaceRegionFactory)
class ArtworkFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Artwork
region = factory.SubFactory(RegionFactory)
有了这个,你应该能够让你的代码工作。注意我们是如何简单地在Region
上设置region
字段的:Django的内部会自动提取对象的内容类型/内容ID。
附加选项你可以调优RegionFactory
,让调用者决定他们是想要SpaceRegion
还是BeaconRegion
:
class RegionFactory(factory.django.DjangoModelFactory):
class Meta:
models = Region
class Params:
space = True # Request a SpaceRegion
region = factory.Maybe(
factory.SelfAttribute("space"),
factory.SubFactory(SpaceRegion),
factory.SubFactory(BeaconRegion),
)