Alter model to add "through" relationship to order a ManytoMany field - Django 1.7 migration modification -
i trying add together order manytomany field created while ago. want order pictures in collections of pictures. running on django 1.7, no more south migrations (i trying follow tutorial: http://mounirmesselmeni.github.io/2013/07/28/migrate-django-manytomany-field-to-manytomany-through-with-south/)
here's "through" relationship have:
class collectionpictures(models.model): image = models.foreignkey( picture, verbose_name=u'picture', help_text=u'picture included in collection.', ) collection = models.foreignkey( collection, verbose_name=u'collection', help_text=u'picture included in collection', ) order = models.integerfield( verbose_name=u'order', help_text=u'what order display image within collection.', max_length=255 ) class meta: verbose_name = u"collection picture" verbose_name_plural = u"collection pictures" ordering = ['order', ] def __unicode__(self): homecoming self.picture.name + " displayed in " + self.collection.name + ( " in position %d" % self.order) class collection(models.model): pictures = models.manytomanyfield(picture, through='collectionpictures', null=true) [... bunch of irrelevant stuff after]
so should work if didn't have migrate old info (the difference in model didn't have through='collectionpictures'
here's migration :
class migration(migrations.migration): dependencies = [ ('artist', '0002_auto_20141013_1451'), ('business', '0001_initial'), ] operations = [ migrations.createmodel( name='collectionpictures', fields=[ ('id', models.autofield(verbose_name='id', serialize=false, auto_created=true, primary_key=true)), ('order', models.integerfield(help_text='what order display image within collection.', max_length=255, verbose_name='order')), ('collection', models.foreignkey(verbose_name='collection', to='business.collection', help_text='picture included in collection')), ('picture', models.foreignkey(verbose_name='picture', to='artist.picture', help_text='picture included in collection.')), ], options={ 'ordering': ['order'], 'verbose_name': 'collection picture', 'verbose_name_plural': 'collection pictures', }, bases=(models.model,), ), migrations.alterfield( model_name='collection', name='pictures', field=models.manytomanyfield(to=b'artist.picture', null=true, through='business.collectionpictures'), ), ]
this throws error when migrating:
valueerror: cannot alter field business.collection.pictures business.collection.pictures - not compatible types (you cannot alter or m2m fields, or add together or remove through= on m2m fields)
has tried kind of manipulation new 1.7 migrations?
thanks !
the safest approach create new field , re-create info over.
leave pictures
lone , add together pictures2
through
field. run makemigrations
.
edit generated migration file , add together runpython
command re-create info old table new table. perhaps can programmatically take value new order
column well.
delete old pictures
field. run makemgirations
.
rename pictures2
pictures
. run makemigrations
.
this approach should leave in state want info intact.
if copying on info big problem seek else, adding order
column in sql, using db_table
alternative on collectionpictures
create point existing table, , wiping out migrations , redoing --fake
. seems riskier approach above.
django migration manytomanyfield django-1.7 django-migrations
No comments:
Post a Comment