测试用例给出错误对象没有属性'_meta'
Test case give error object has no attribute '_meta'
我有以下 Django 测试用例:
objects = ActionManager()
action = objects.log_action(user=self.test_user)
self.assertIsInstance(action, Action)
但是,由于我在上面的例子中访问管理器的方式非常规,我得到了这个错误:
app = model._meta.app_label
AttributeError: 'NoneType' object has no attribute '_meta'
经理:
class ActionManager(models.Manager):
def log_action(self, user, content_object):
action = self.model(
user=user,
)
user.save(using=self._db)
return action
我认为这样做的原因是因为管理器没有以 Action.objects.log_action
的方式附加到模型,所以 self.model
不起作用。这就是我假设正在发生的事情。
我的问题是,如何在保持 self.model
和我的测试用例完好无损的情况下解决这个问题?
没有理由不能自己设置模型。
objects = ActionManager()
objects.model = MyModel
我有以下 Django 测试用例:
objects = ActionManager()
action = objects.log_action(user=self.test_user)
self.assertIsInstance(action, Action)
但是,由于我在上面的例子中访问管理器的方式非常规,我得到了这个错误:
app = model._meta.app_label
AttributeError: 'NoneType' object has no attribute '_meta'
经理:
class ActionManager(models.Manager):
def log_action(self, user, content_object):
action = self.model(
user=user,
)
user.save(using=self._db)
return action
我认为这样做的原因是因为管理器没有以 Action.objects.log_action
的方式附加到模型,所以 self.model
不起作用。这就是我假设正在发生的事情。
我的问题是,如何在保持 self.model
和我的测试用例完好无损的情况下解决这个问题?
没有理由不能自己设置模型。
objects = ActionManager()
objects.model = MyModel