Search code examples
pythondjangodjango-managers

Test case give error object has no attribute '_meta'


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?


Solution

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

    objects = ActionManager()
    objects.model = MyModel