11

I am trying to formulate a datamigration for one of my apps. I am using the reputation system mentioned here - django-reputation

in my forward method, I have the following code -

orm['reputation.reputation'].objects.log_reputation_action(user = user_x, originating_user = user_y, action_value = 10, target_object = sample_obj)

but while running the migration, I get the following error -

AttributeError: 'Manager' object has no attribute 'log_reputation_action'

I have freezed the reputation app in the datamigration. Please let me know what I am doing wrong here.

Thanks in advance.

3 Answers 3

9

Looks like this is not possible.

From the South documentation:

You can do a lot more with this inside a data migration; any model can be available to you. The only caveat is that you won’t have access to any custom methods or managers on your models, as they’re not preserved as part of the freezing process (there’s no way to do this generally); you’ll have to copy any code you want into the migration itself. Feel free to make them methods on the Migration class; South ignores everything apart from forwards and backwards.

2
  • 1
    guess I would have to have to do it the ugly way. thanks for the citation!! Dec 14, 2014 at 14:49
  • If you are only going to migrate forwards - you can import from the real models. Just beware that it will mean that this migration can only be applied now. I've used this with a datamigration taking a complicated calculated thing and turning that into a real column. Feb 1, 2016 at 16:14
4

Since Django 1.8, you can include model managers in migrations by adding the use_in_migrations property.

From the docs: https://docs.djangoproject.com/en/2.0/topics/migrations/#model-managers

class MyManager(models.Manager):
    use_in_migrations = True

class MyModel(models.Model):
    objects = MyManager()
2
  • 1
    Just make sure to read up on the consequences of this approach first. "References to [...] and model manager declarations with managers having use_in_migrations = True are serialized in migrations, so the functions and classes will need to be kept around for as long as there is a migration referencing them." Excerpt from the official documentation with custom highlights.
    – finngu
    Oct 27, 2020 at 13:25
  • Does someone have a full example of this to share please ?
    – vhamon
    Dec 11, 2020 at 14:43
2

Augmenting @mattdedek's answer with an example of use in a migration

def my_migration_function(apps, schema_editor):
    MyModel = apps.get_model('my_app_name', 'MyModel')
    MyModel.objects.create(name='foo')


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        ...
    ]

    operations = [
        migrations.RunPython(my_migration_function),
    ]

Currently works in django migrations (tested on version 3.0.4)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.