Python 工厂男孩与Django许多模型

Python 工厂男孩与Django许多模型,python,django,factory-boy,Python,Django,Factory Boy,我在同一模块上有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.TextFi

我在同一模块上有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
车辆

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
我正在尝试使用此模型创建装置

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

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
)
当我运行测试时,我得到这个错误

ValueError:“”需要有字段“id”的值,才能使用此多对多关系。

我读过工厂的文档,但无法修复

调用UserFactory()或UserFactory.build()时,没有组绑定 将创建

但是当UserFactory.create(groups=(group1、group2、group3))为 调用时,groups声明会将传入的组添加到 用户的组

id
字段将在

optional = OptionalFactory.create(  # note the .create()
    name="GPS",
    type="13",
    description="",
    charge=charge,
    provider=provider
)
vehicle = VehicleFactory.create(
    ...
    optionals=(optional,),
)
那么当你

optional = OptionalFactory.create(  # note the .create()
    name="GPS",
    type="13",
    description="",
    charge=charge,
    provider=provider
)
vehicle = VehicleFactory.create(
    ...
    optionals=(optional,),
)

可以建立多对多
选项
。注意optionals的参数是
(optionals,)
。该函数需要一个iterable

您已经在文档中指向了正确的位置,应该可以:

然后你会打电话说:

VehicleFactory.create(optionals=[optional1, optional2])
希望这能回答这个问题?如果没有更多关于在文档中尝试解决方案时哪些不起作用的信息,就很难提供更多帮助