Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing model manager methods in datamigration

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.

like image 208
Keval Doshi Avatar asked Nov 23 '14 18:11

Keval Doshi


2 Answers

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.

like image 187
Amit Avatar answered Oct 21 '22 01:10

Amit


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()
like image 26
mattdedek Avatar answered Oct 21 '22 00:10

mattdedek