samedi 25 juin 2016

how to create a django migration for splitting model into multiple models


I have a django model that I am splitting into multiple models (django 1.9) The app is more or less a system for tracking items and sets of items, which are usually associated with a file. (these files are not uploaded, or added by users, this is why I do no use the file field)

Here is a toy example of one of the items, which also has an associated file:

class ItemTypeA(models.Model):
    field1 = models.CharField(max_length=512)
    ...
    base_dir = models.CharField(max_length=512)
    rel_fname = models.CharField(max_length=512)

In that model, I can filter on field1 and then get the appropriate file, (the real model has many fields, and relationships with other models not listed). There are also many more types of Items.

I want to move the file details another model with a onetoone relationship.

This might look like this:

class Filesystem(models.Model):
    dirpath = models.CharField(max_length=512)
    ... #Some other details for accessing the system, and system availablity.

class BaseFile(models.Model):
    filesystem = models.ForeignKey(Filesystem,related_name="system")
    filepath = models.CharField(max_length=512)
    md5 = models.CharField(max_length=16, editable=False)
    md5_check_datetime = models.DateTimeField()
    size = models.IntegerField(verbose_name="file_size")
    class Meta:
        abstract = True

class ItemTypeAFile(BaseFile):
    pass

class ItemTypeA(models.Model):
    field1 = models.CharField(max_length=512)
    file = models.OneToOneField(ItemTypeAFile)

I would like to create a migration for this such that filesystems are created when for each unique base_dir==>dirpath, and a new ItemTypeAFile object is created with OldItemTypeA.rel_fname==>ItemTypeAFile.filepath.

I do not really even know where to start with this.


Aucun commentaire:

Enregistrer un commentaire