Skip to content

Instantly share code, notes, and snippets.

@jMyles
Created November 1, 2014 22:33
Show Gist options
  • Save jMyles/130050563ba96a7d7cc8 to your computer and use it in GitHub Desktop.
Save jMyles/130050563ba96a7d7cc8 to your computer and use it in GitHub Desktop.
'''
Mezzanine lets you inject fields on its own stock models using the EXTRA_MODEL_FIELDS setting.
However, as the Mezzanine docs point out (http://mezzanine.jupo.org/docs/model-customization.html#field-injection-caveats), there are some "caveats" regarding migrations.
Specifically, the migrations need to be placed in a local app even though they're applied to the Mezzanine models.
What's the proper way to do this in Django 1.7? I have a solution below that works, but it's obviously ugly.
'''
### Here's the output of makemigrations:
Migrations for 'pages':
0002_auto_20141031_1932.py:
- Add field theme to page
- Alter field in_menus on page
Full migrations file '0002_auto_20141031_1932.py':
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import mezzanine.pages.fields
class Migration(migrations.Migration):
dependencies = [
('blogging', '0002_theme'),
('pages', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='page',
name='theme',
field=models.ForeignKey(blank=True, to='blogging.Theme', null=True),
preserve_default=True,
),
]
### And here's my monkeypatch to make it work properly:
class AddFieldToOtherApp(migrations.AddField):
def __init__(self, app_name, *args, **kwargs):
self.app_name = app_name
return super(AddFieldToOtherApp, self).__init__(*args, **kwargs)
def state_forwards(self, app_label, state):
return super(AddFieldToOtherApp, self).state_forwards(self.app_name, state)
class Migration(migrations.Migration):
dependencies = [
('blogging', '0002_theme'),
('pages', '0001_initial'),
]
operations = [
AddFieldToOtherApp(
app_name='pages',
model_name='page',
name='theme',
field=models.ForeignKey(blank=True, to='blogging.Theme', null=True),
preserve_default=True,
),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment