gpt4 book ai didi

python - Factory Boy 与 Django ManyToMany 模型

转载 作者:行者123 更新时间:2023-12-01 07:39:35 26 4
gpt4 key购买 nike

我在同一模块上有 3 个模型,app.models.py ,如下。其他一些模型可能会出现在代码中,但并不相关。

可选

class Optional(models.Model):
name = models.CharField(_('Nome'), max_length=255)
type = models.CharField(_('Tipo'), max_length=50, null=True, blank=True)
description = models.TextField(_('Descrição'), null=True, blank=True)
provider = models.ForeignKey('providers.Provider', null=True, blank=True)
charge = models.ForeignKey('Charge', null=True, blank=True)

def __str__(self):
return self.name

覆盖范围

class Coverage(models.Model):
code = models.CharField(_('Código'), max_length=20, null=True, blank=True)
vehicle_code = models.CharField(_('Código do veículo (ACRISS)'), max_length=4, null=True, blank=True)
charge = models.ForeignKey('Charge', null=True, blank=True)

车辆

class Vehicle(models.Model):

code = models.CharField(_('Código'), max_length=100)
description = models.CharField(_('Descrição'), max_length=255, null=True, blank=True)
model = models.CharField(_('Modelo'), max_length=100, null=True, blank=True)
brand = models.CharField(_('Fabricante'), max_length=100, null=True, blank=True)
group = models.ForeignKey('Group', null=True, blank=True)
optionals = models.ManyToManyField('Optional', related_name='vehicle_optional')
coverages = models.ManyToManyField('Coverage', related_name='vehicle_coverage')

def __str__(self):
return self.code

我正在尝试使用 factory_boy 从该模型创建灯具.

class CoverageFactory(factory.Factory):
class Meta:
model = Coverage
charge = factory.SubFactory(ChargeFactory)

class OptionalFactory(factory.Factory):
class Meta:
model = Optional
provider = factory.SubFactory(ProviderFactory)
charge = factory.SubFactory(ChargeFactory)

class VehicleFactory(factory.Factory):
class Meta:
model = Vehicle
group = factory.SubFactory(GroupFactory)
optionals = factory.SubFactory(OptionalFactory)
coverages = factory.SubFactory(CoverageFactory)

在我的测试中,它是这样实例化的:

optional = OptionalFactory(
name="GPS",
type="13",
description="",
charge=charge,
provider=provider
)

coverage = CoverageFactory(
code="ALI",
vehicle_code="ABCD",
charge=charge
)

vehicle = VehicleFactory(
code="ECMM",
description="GRUPO AX - MOVIDA ON",
model="MOBI LIKE, OU SIMILAR",
brand="",
optionals=optional,
coverages=coverage
)

当我运行测试时,使用 pytest-django ,我收到此错误。

ValueError: "<Vehicle: ECMM>" needs to have a value for field "id" before this many-to-many relationship can be used.

我已阅读有关Simple Many-to-many relationship的factory_boy文档和 Many-to-many relation with a ‘through’但无法修复。

最佳答案

When calling UserFactory() or UserFactory.build(), no group binding will be created.

But when UserFactory.create(groups=(group1, group2, group3)) is called, the groups declaration will add passed in groups to the set of groups for the user.

id 字段将在您

时生成
optional = OptionalFactory.create(  # note the .create()
name="GPS",
type="13",
description="",
charge=charge,
provider=provider
)

然后当你

vehicle = VehicleFactory.create(
...
optionals=(optional,),
)

可以建立多对多可选。还请注意,可选参数是(可选,)。该函数需要一个可迭代的

关于python - Factory Boy 与 Django ManyToMany 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56798656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com