Django:在模型上模拟字段 [英] Django: Mock a field on a model
本文介绍了Django:在模型上模拟字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在该模型的用户字段上分配模拟对象?反正绕过"SomeModel.user"必须是"User"实例"检查吗?
How do I assign a mock object to the user field on this model? Is the anyway to bypass the '"SomeModel.user" must be a "User" instance' check?
class SomeModel(models.Model):
user = models.ForeignKey(User)
我并没有保存该模型,它仅用于对该模型上的方法进行单元测试.我也不想创建一个真实的用户模型.
I am not saving this model it's purely for unit testing a method on that model. I also don't want to create a real User model.
推荐答案
您不能真正绕过该检查.您可以获得的最接近的是:
You can't really bypass that check. The closest you can get is:
class SomeModel(models.Model):
user_id = models.IntegerField(max_length=10)
这篇关于Django:在模型上模拟字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文