簡體   English   中英

django-factory_boy AttributeError:“ NoneType”對象沒有屬性“ _meta”

[英]django - factory_boy AttributeError: 'NoneType' object has no attribute '_meta'

我正在為Django REST Framework應用程序編寫單元測試,並使用factory_boy創建虛假的測試數據。 嘗試運行測試時遇到以下錯誤消息

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'

我幾乎不知道發生了什么事,因為我對后端結構的理解不是很好。 這是工廠:

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

這是我稱呼他們的地方:

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()
        )

如果您查看錯誤消息,則特別是抱怨contributor = UserFactory()

讓我知道是否有一個簡單的解決方法,或者對發生的事情進行一些解釋就可以了。

非常感謝!

這是文件:

tests.py

您首先需要在工廠定義中添加正確的關系( SubFactory )。

請仔細閱讀以下部分:

http://factoryboy.readthedocs.io/zh-CN/latest/recipes.html#copying-fields-to-a-subfactory

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM