Skip to content

Make manager available in RunPython #143

@ptrstn

Description

@ptrstn

Hello,

first of all thank your for this django package. I am in the process of replacing my own Category models with this package.

I was trying to write a data migration, but ran into a problem when trying to access the object manager in the Category model

NewCategory = apps.get_model('categories', 'Category')
...
NewCategory.objects.create(name=old_category.name)

caused following Error message:

AttributeError: type object 'Category' has no attribute 'objects'

I found out that in order to use a custom manager in RunPython data migrations you have to set the use_in_migrations attribute to True, as described here: https://docs.djangoproject.com/en/1.11/topics/migrations/#model-managers

In your package it would mean

class CategoryManager(models.Manager):
    """
    A manager that adds an "active()" method for all active categories
    """
    use_in_migrations = True

    def active(self):
        """
        Only categories that are active
        """
        return self.get_queryset().filter(active=True)

and maybe even the same to the CategoryRelationManager.

If you agree with this proposal, I could write a pull request if you wish.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions