0

I have the following Django test case:

objects = ActionManager()
action = objects.log_action(user=self.test_user)
self.assertIsInstance(action, Action)

However, because of the unconventional way I'm accessing the manager in the above example I get this error:

app = model._meta.app_label

AttributeError: 'NoneType' object has no attribute '_meta'

Manager:

class ActionManager(models.Manager):

    def log_action(self, user, content_object):
        action = self.model(
            user=user,
        )
        user.save(using=self._db)
        return action

The reason for this I assume is because the Manager is not attached to a model in the way Action.objects.log_action so self.model does not work. This is what I assume is happening.

My question is, how can I resolve this issue while keeping self.model and my test case in tact?

Community
  • 1
  • 1
Prometheus
  • 32,405
  • 54
  • 166
  • 302
  • 2
    Can you explain why you want to do this, rather than putting the `objects` definition inside the model as usual? – Daniel Roseman May 06 '15 at 14:10
  • @DanielRoseman only because I was following this guide on how to test Managers in Django: https://bradmontgomery.net/blog/2013/04/07/django-manager-testing-woes/ – Prometheus May 06 '15 at 14:13

1 Answers1

0

There's no reason why you can't set the model yourself.

objects = ActionManager()
objects.model = MyModel
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895