在Django中大量创建数据

在Django中大量创建数据,django,Django,我有一个模型单元 class Unit(CommonInfo): version = IntegerVersionField( ) number = models.CharField(max_length=30,null=True, blank=True) max_occupants = models.PositiveSmallIntegerField() floor = models.PositiveSmallIntegerField() rooms =

我有一个模型单元

class Unit(CommonInfo):
    version = IntegerVersionField( )
    number = models.CharField(max_length=30,null=True, blank=True)
    max_occupants = models.PositiveSmallIntegerField()
    floor = models.PositiveSmallIntegerField()
    rooms = models.PositiveSmallIntegerField()
    is_disabled_access = models.BooleanField(default=False)
    balcony_quantity = models.PositiveSmallIntegerField()
    building = models.ForeignKey(Building)
    recomended_price = models.DecimalField(max_digits=7, decimal_places=2)
每个单元都有属性表中定义的所有属性

class Property1(CommonInfo):
    unit = models.ForeignKey(Unit)
    is_true = models.BooleanField(default=False)
    propertytype = models.ForeignKey(Propertytype, related_name='propertytype')
    date = models.DateTimeField(null=True, blank=True)
    followup_date = models.DateTimeField(null=True, blank=True)
    quantity = models.PositiveSmallIntegerField()

是否有一种方法可以将所有属性批量创建到具有某些默认值的所有单元?

如果这是针对测试代码,请使用factory boy
RelatedFactory
执行。看见避免使用固定装置

如果这是一项一次性任务,您需要对预先存在的数据执行,请编写数据迁移。看


如果您希望在创建
单元时自动执行此操作,请使用post\u save signal处理程序执行此操作。看

Django有一个
fixtures
功能。用它来澄清你的问题。当你说“单位”时,你是指作为外键生成的实际单位模型吗?