[英]django - factory_boy AttributeError: 'NoneType' object has no attribute '_meta'
I'm writing unit tests for my Django REST Framework
app and I'm creating my fake testing data using factory_boy
. 我正在为
Django REST Framework
应用程序编写单元测试,并使用factory_boy
创建虚假的测试数据。 I've come across the following error message when I try to run my tests 尝试运行测试时遇到以下错误消息
File "/Users/thomasheatwole/osf-meetings/meetings/conferences/tests.py", line 69, in setUp
contributor = UserFactory()
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 67, in __call__
return cls.create(**kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 594, in create
return cls._generate(True, attrs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 519, in _generate
obj = cls._prepare(create, **attrs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 494, in _prepare
return cls._create(model_class, *args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/django.py", line 181, in _create
return manager.create(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 745, in save_base
update_fields=update_fields, raw=raw, using=using)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 192, in send
response = receiver(signal=self, sender=sender, **named)
File "/Users/thomasheatwole/osf-meetings/meetings/submissions/signals.py", line 19, in add_permissions_on_submission_save
submission, submission_contributor, conference_admin, approval)
File "/Users/thomasheatwole/osf-meetings/meetings/submissions/permissions.py", line 167, in set_unapproved_submission_permissions
approval, submission_contributor)
File "/Users/thomasheatwole/osf-meetings/meetings/approvals/permissions.py", line 62, in add_approval_permissions_to_submission_contributor
assign_perm("approvals.delete_approval", submission_contributor, approval)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/guardian/shortcuts.py", line 92, in assign_perm
return model.objects.assign_perm(perm, user, obj)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/guardian/managers.py", line 43, in assign_perm
obj_perm, created = self.get_or_create(**kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 467, in get_or_create
return self._create_object_from_params(lookup, params)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 499, in _create_object_from_params
obj = self.create(**params)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/guardian/models.py", line 39, in save
content_type = ContentType.objects.get_for_model(self.content_object)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 55, in get_for_model
opts = self._get_opts(model, for_concrete_model)
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 32, in _get_opts
model = model._meta.concrete_model
AttributeError: 'NoneType' object has no attribute '_meta'
I pretty much have no clue what's going on since my understanding of backend structure isn't great. 我几乎不知道发生了什么事,因为我对后端结构的理解不是很好。 Here's the factories:
这是工厂:
class UserFactory(factory.DjangoModelFactory):
class Meta:
model = User
class ConferenceFactory(factory.DjangoModelFactory):
class Meta:
model = Conference
class ApprovalFactory(factory.DjangoModelFactory):
class Meta:
model = approvalModels.Approval
class SubmissionFactory(factory.DjangoModelFactory):
class Meta:
model = submissionModels.Submission
And here's where I call them: 这是我称呼他们的地方:
def setUp(self):
self.user1 = UserFactory(
username = 'Leo',
id = '99'
)
self.user2 = UserFactory(
username = 'LeoLeo'
)
self.conference = ConferenceFactory(
admin = self.user1
)
self.submission1 = SubmissionFactory(
conference = self.conference,
contributor = UserFactory()
)
self.submission2 = SubmissionFactory(
conference = self.conference,
contributor = UserFactory()
)
If you look through the error message, it's specifically complaining about contributor = UserFactory()
如果您查看错误消息,则特别是抱怨
contributor = UserFactory()
Let me know if there's an easy fix, or even some explanation of what's going on would be nice. 让我知道是否有一个简单的解决方法,或者对发生的事情进行一些解释就可以了。
Thanks so much! 非常感谢!
Here's the file: 这是文件:
You need to add correct relations ( SubFactory
) in the factory definition first. 您首先需要在工厂定义中添加正确的关系(
SubFactory
)。
Please read this part carefully: 请仔细阅读以下部分:
http://factoryboy.readthedocs.io/en/latest/recipes.html#copying-fields-to-a-subfactory http://factoryboy.readthedocs.io/zh-CN/latest/recipes.html#copying-fields-to-a-subfactory
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.