开发者社区> 问答> 正文

ImportError:无法导入名称'Message' - django-messages

我分叉了https://github.com/arneb/django-messages/并把它放在我的仓库中:https://github.com/mike-johnson-jr/django-messages/

当我使用该包时,我在标题中收到错误。完全追溯:

Traceback (most recent call last):
File "manage.py", line 15, in

execute_from_command_line(sys.argv)

File "/home/michael/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line

utility.execute()

File "/home/michael/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute

django.setup()

File "/home/michael/.local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup

apps.populate(settings.INSTALLED_APPS)

File "/home/michael/.local/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate

app_config.import_models()

File "/home/michael/.local/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models

self.models_module = import_module(models_module_name)

File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/michael/.local/lib/python3.6/site-packages/django_messages/models.py", line 48, in

class Message(models.Model):

File "/home/michael/.local/lib/python3.6/site-packages/django_messages/models.py", line 87, in Message

get_absolute_url = reverse(get_absolute_url)

File "/home/michael/.local/lib/python3.6/site-packages/django/urls/base.py", line 90, in reverse

return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

File "/home/michael/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 562, in _reverse_with_prefix

self._populate()

File "/home/michael/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 413, in _populate

for url_pattern in reversed(self.url_patterns):

File "/home/michael/.local/lib/python3.6/site-packages/django/utils/functional.py", line 37, in get

res = instance.__dict__[self.name] = self.func(instance)

File "/home/michael/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns

patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/home/michael/.local/lib/python3.6/site-packages/django/utils/functional.py", line 37, in get

res = instance.__dict__[self.name] = self.func(instance)

File "/home/michael/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module

return import_module(self.urlconf_name)

File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/michael/projects/datafix/datafix/urls.py", line 65, in

path('messages/', include('django_messages.urls')),

File "/home/michael/.local/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include

urlconf_module = import_module(urlconf_module)

File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/michael/.local/lib/python3.6/site-packages/django_messages/urls.py", line 4, in

from django_messages.views import *

File "/home/michael/.local/lib/python3.6/site-packages/django_messages/views.py", line 11, in

from django_messages.models import Message

ImportError: cannot import name 'Message'
这里是我的django_messages.models代码:

@python_2_unicode_compatible
class Message(models.Model):

"""
A private message from user to user
"""
subject = models.CharField(_("Subject"), max_length=140)
body = models.TextField(_("Body"))
sender = models.ForeignKey(AUTH_USER_MODEL, related_name='sent_messages', verbose_name=_(
    "Sender"), on_delete=models.SET_NULL)
recipient = models.ForeignKey(AUTH_USER_MODEL, related_name='received_messages',
                              null=True, blank=True, verbose_name=_("Recipient"), on_delete=models.SET_NULL)
parent_msg = models.ForeignKey('self', related_name='next_messages', null=True,
                               blank=True, verbose_name=_("Parent message"), on_delete=models.SET_NULL)
sent_at = models.DateTimeField(_("sent at"), null=True, blank=True)
read_at = models.DateTimeField(_("read at"), null=True, blank=True)
replied_at = models.DateTimeField(_("replied at"), null=True, blank=True)
sender_deleted_at = models.DateTimeField(
    _("Sender deleted at"), null=True, blank=True)
recipient_deleted_at = models.DateTimeField(
    _("Recipient deleted at"), null=True, blank=True)

objects = MessageManager()

展开
收起
一码平川MACHEL 2019-01-23 17:03:26 3362 0
3 条回答
写回答
取消 提交回答
  • Artisan

    import 出错一般就一个问题:循环引用,你前面定义message model的代码逻辑间接依赖import model

    2019-07-17 23:26:45
    赞同 展开评论 打赏
  • ImportError:一般是文件或库的目录没有设置

    2019-07-17 23:26:45
    赞同 展开评论 打赏
  • 错误出现在我的django_messages.models.Message代码中,get_absolute_url()很不稳定。

    2019-07-17 23:26:45
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载