Question: Mass creation of data in Django


Mass creation of data in Django

Answers 1
Added at 2016-12-28 21:12

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?

Answers to

Mass creation of data in Django

nr: #1 dodano: 2016-12-28 21:12

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.

Source Show
◀ Wstecz