Ilya Bibik
Ilya Bibik

Reputation: 4114

Mass creation of data in Django

I have a model Unit.

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)

Each Unit has all property/s defined in Property table

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()

Is there a way to mass create all the property to all the units with some default value?

Upvotes: 2

Views: 89

Answers (1)

wim
wim

Reputation: 362488

If this is for test code, do it with factory boy's RelatedFactory. See here. Avoid fixtures.

If this is a once-off task you need to do on pre-existing data, write a data migration. See here.

If this is something you want to happen automatically whenever a Unit is created, do it using a post_save signal handler. See here.

Upvotes: 2

Related Questions