Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make manager available in RunPython #143

Open
ptrstn opened this issue Jul 5, 2018 · 0 comments
Open

Make manager available in RunPython #143

ptrstn opened this issue Jul 5, 2018 · 0 comments

Comments

@ptrstn
Copy link

ptrstn commented Jul 5, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant