Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing south to refactor a Django model with inheritance
    primarykey
    data
    text
    <p>I was wondering if the following migration is possible with Django <a href="http://south.aeracode.org/" rel="nofollow noreferrer">south</a> and still retain data.</p> <h2>Before:</h2> <p>I currently have two apps, one called tv, one called movies, each with a VideoFile model (simplified here):</p> <p><strong>tv/models.py:</strong></p> <pre><code>class VideoFile(models.Model): show = models.ForeignKey(Show, blank=True, null=True) name = models.CharField(max_length=1024, blank=True) size = models.IntegerField(blank=True, null=True) ctime = models.DateTimeField(blank=True, null=True) </code></pre> <p><strong>movies/models.py:</strong></p> <pre><code>class VideoFile(models.Model): movie = models.ForeignKey(Movie, blank=True, null=True) name = models.CharField(max_length=1024, blank=True) size = models.IntegerField(blank=True, null=True) ctime = models.DateTimeField(blank=True, null=True) </code></pre> <h2>After:</h2> <p>Because the two videofile objects are so similar I want to get rid of duplication and create a new model in a separate app called media that contains a generic VideoFile class and use inheritance to extend it:</p> <p><strong>media/models.py:</strong></p> <pre><code>class VideoFile(models.Model): name = models.CharField(max_length=1024, blank=True) size = models.IntegerField(blank=True, null=True) ctime = models.DateTimeField(blank=True, null=True) </code></pre> <p><strong>tv/models.py:</strong></p> <pre><code>class VideoFile(media.models.VideoFile): show = models.ForeignKey(Show, blank=True, null=True) </code></pre> <p><strong>movies/models.py:</strong></p> <pre><code>class VideoFile(media.models.VideoFile): movie = models.ForeignKey(Movie, blank=True, null=True) </code></pre> <p>So my question is, how can I accomplish this with django-south and still maintain existing data?</p> <p>All three these apps are already managed by south migrations and according to the south documentation it is bad practice to combine a schema and data migration and they recommend it should be done in a few steps.</p> <p>I think it could be done using separate migrations like this (assuming media.VideoFile is already created)</p> <ol> <li>Schema migration to rename all fields in tv.VideoFile and movies.VideoFile that will move to the new media.VideoFile model, maybe to something like old_name, old_size, etc</li> <li>Schema migration to tv.VideoFile and movies.VideoFile to inherit from media.VideoFile</li> <li>Data migration to copy old_name to name, old_size to size, etc</li> <li>Scheme migration to remove old_ fields</li> </ol> <p>Before I go through all that work, do you think that will work? Is there a better way?</p> <p>If you're interested, the project is hosted here: <a href="http://code.google.com/p/medianav/" rel="nofollow noreferrer">http://code.google.com/p/medianav/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload