15👍
You should be able to do this pretty easily.
First of all, make sure that the manual through table that you are creating has the same table name in the database as the one Django originally created automatically.
So, first, let’s consider a manual through model before your change:
class AUsers(models.Model):
user = models.ForeignKey('auth.User')
a = models.ForeignKey('A')
class Meta:
db_table = 'appname_a_user'
That should be functionally (almost) identical to the ManyToManyField
you used to have. Actually, you could make an empty migration and apply it, and then use –auto for your changes (but don’t).
Now, add your field like you did in your sample code above, and then run ./manage.py schemamigration appname manual_through_table --empty
. That will give you an empty migration named ####_manual_through_table.py
.
In the migration itself, there will be a forwards
and backwards
method. Each one needs to be one line each:
def forwards(self, orm):
db.add_column('appname_a_user', 'new_field', self.gf('django.db.models.fields.BooleanField')(default=False))
def backwards(self, orm):
db.delete_column('appname_a_user', 'new_field')
That should get you what you are after.
7👍
If anyone comes across this question when trying to do the same thing with the moderns migration framework, here are the steps:
- Create a new model class that exactly matches the built-in through table
- Use the Meta class to set the table name to match the existing table
- Generate a migration, which will create the new table and set it as the through for the field.
- Without running that migration, edit it to wrap it in a
migrations. SeparateDatabaseAndState
migration, where the auto-generated steps are in thestate_operations
field and the database operations are empty. - Modify your through table, as required, making sure to generate new migrations as normal.
0👍
As mentioned in a comment, the first step may be simplified using db.rename_table
as described here, which gives this through model:
class AUsers(models.Model):
user = models.ForeignKey('auth.User')
a = models.ForeignKey('A')
class Meta:
unique_together = (('user', 'a'),)
Then, create a migration with –auto (this way you’ll have the names of the DB tables visible), and replace the content with:
class Migration(SchemaMigration):
def forwards(self, orm):
db.rename_table('appname_a_user', 'appname_auser')
def backwards(self, orm):
db.rename_table('appname_auser','appname_a_user')
I just applied it in my project without issues.
- How to import a json file to a Django model?
- Error loading MySQLdb module: libmysqlclient.so.20: cannot open shared object file: No such file or directory
- Enable PK based filtering in Django Graphene Relay while retaining Global IDs
- Django: duplicates when filtering on many to many field
- List filter by custom list display field in django admin