简体   繁体   English

Django rest-api - 属性错误:'str' object 没有属性 '_meta'

[英]Django rest-api - attributeerror: 'str' object has no attribute '_meta'

I'm getting the following error while running manage.py migrate in my Django rest-api.在我的 Django rest-api 中运行 manage.py migrate 时出现以下错误。 I'm unable to pinpoint where to look.我无法确定在哪里看。 Help appreciated.帮助表示赞赏。

attributeerror: 'str' object has no attribute '_meta'属性错误:“str”object 没有属性“_meta”

The traceback shows the following:回溯显示以下内容:

'' Traceback (most recent call last): File "/home/myproject/myproject-api/manage.py", line 25, in execute_from_command_line(sys.argv) '' Traceback(最近一次调用最后):文件“/home/myproject/myproject-api/manage.py”,第 25 行,在 execute_from_command_line(sys.argv)

File "/home/myEnv/lib/python3.9/site-packages/django/core/management/ init .py", line 419, in execute_from_command_line utility.execute()文件“ /home/myEnv/lib/python3.9/site-packages/django/core/management/init .py”,第 419 行,在 execute_from_command_line utility.execute()

File "/home/myEnv/lib/python3.9/site-packages/django/core/management/ init .py", line 395, in execute django.setup()文件“ /home/myEnv/lib/python3.9/site-packages/django/core/management/init .py”,第 395 行,在执行 django.setup()

File "/home/myEnv/lib/python3.9/site-packages/django/ init .py", line 24, in setup apps.populate(settings.INSTALLED_APPS)文件“ /home/myEnv/lib/python3.9/site-packages/django/init .py”,第 24 行,在 setup apps.populate(settings.INSTALLED_APPS)

File "/home/myEnv/lib/python3.9/site-packages/django/apps/registry.py", line 122, in populate app_config.ready()文件“/home/myEnv/lib/python3.9/site-packages/django/apps/registry.py”,第 122 行,在填充 app_config.ready()

File "/home/myEnv/lib/python3.9/site-packages/cacheops/ init .py", line 18, in ready install_cacheops()文件“ /home/myEnv/lib/python3.9/site-packages/cacheops/init .py”,第 18 行,准备好 install_cacheops()

File "/home/myEnv/lib/python3.9/site-packages/funcy/flow.py", line 231, in wrapper return func(*args, **kwargs)包装器中的文件“/home/myEnv/lib/python3.9/site-packages/funcy/flow.py”,第 231 行,返回 func(*args, **kwargs)

File "/home/myEnv/lib/python3.9/site-packages/cacheops/query.py", line 578, in install_cacheops opts = rel.through._meta AttributeError: 'str' object has no attribute '_meta'文件“/home/myEnv/lib/python3.9/site-packages/cacheops/query.py”,第 578 行,在 install_cacheops opts = rel.through._meta AttributeError: 'str' object has no attribute '_meta'

'' ''

The issue was this.问题是这样的。 I had to install我不得不安装

pip install django-cacheops==0.9.3 pip 安装 django-cacheops==0.9.3

This should be helpful for anyone who encounters the exact above error.这对遇到上述确切错误的任何人都有帮助。

The error you get from cacheops seems to have to do with the fact that you're using a many-to-many relationship with a through model, where you specify the through model by passing a string with the class's name instead of the class itself, because the class is only defined below.您从 cacheops 获得的错误似乎与您使用与through model 的多对多关系这一事实有关,您可以through传递带有类名称的字符串而不是 ZA2F35E630DAF44ABC2CBBDC212ED4F8EBC2CBBDC212ED4F8EBC26BBDC212ED4F8EBC22 ,因为 class 仅在下面定义。 Although this is perfectly common practice, cacheops seems to require that you do it differently:尽管这是非常普遍的做法,但 cacheops 似乎要求您以不同的方式进行操作:

Namely, try to reference your through model by passing the class instead of a string with the class's name.即,尝试through传递 class 而不是带有类名的字符串来引用您的 model。 For this, you need to reorder your code a bit, putting the class definition of your through model first, which of course requires you to pass strings instead of classes to ForeignKey there.为此,您需要稍微重新排序您的代码,首先将您through model 的 class 定义放入其中,这当然需要您将字符串而不是类传递给ForeignKey那里。 Eg do it like this:例如这样做:

class PostCategory(models.Model):
    post = models.ForeignKey('Post')
    category = models.ForeignKey('Category')

class Post(models.Model):
    pass

class Category(models.Model):
    posts = models.ManyToManyField(Post, through=PostCategory, related_name='categories')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django 中的迁移错误:AttributeError: 'str' object 没有属性 '_meta' - Migrations Error in Django: AttributeError: 'str' object has no attribute '_meta' Django 抛出 AttributeError: 'str' object has no attribute '_meta' when I register my Model - Django throws AttributeError: 'str' object has no attribute '_meta' when I register my Model AttributeError: 'str' object 没有属性 '_meta' / Python 3.6 - AttributeError: 'str' object has no attribute '_meta' / Python 3.6 Django Python3-AttributeError:“模块”对象没有属性“ META” - Django Python3 - AttributeError: 'module' object has no attribute 'META' 'opts = self.remote_field.model._meta' 'AttributeError: 'str' 对象没有属性 '_meta'' - 'opts = self.remote_field.model._meta' 'AttributeError: 'str' object has no attribute '_meta'' 我有这样的错误 “AttributeError at /uz/account/update/ 'str' object has no attribute '_meta' ” - I have a mistake like this “AttributeError at /uz/account/update/ 'str' object has no attribute '_meta' ” AttributeError:“ TestPoll”对象没有属性“ factory”,django rest框架,测试 - AttributeError: 'TestPoll' object has no attribute 'factory', django rest framework, tests Django REST:AttributeError:“ WSGIRequest”对象没有属性“ session” - Django REST: AttributeError: 'WSGIRequest' object has no attribute 'session' Django-Rest-Framework'str'对象没有属性'id' - Django-Rest-Framework 'str' object has no attribute 'id' 使用Django Rest Framework,“ str”对象没有属性“ items” - 'str' object has no attribute 'items' using Django Rest Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM