简体   繁体   English

Python 无法导入名称 Django

[英]Python Cannot Import Name Django

I get the following error我收到以下错误

File "/mnt/d/Protostar/server/src/api/ideas/serializers.py", line 2, in <module>                                     
from .models import Idea                                                                                            
ImportError: cannot import name 'Idea'   

models.py:模型.py:

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Idea(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    title = models.CharField(max_length=50)

serializers.py:序列化程序.py:

from rest_framework import serializers
from .models import Idea

class IdeaSerializer(serializers.ModelSerializer):
    class Meta:
        model = Idea
        fields = '__all__'

I have no idea why this isn't working, I've imported the same way in many projects before.我不知道为什么这不起作用,我以前在许多项目中都以相同的方式导入过。

Here is the directory:这是目录: 在此处输入图像描述

It turns out I have been importing from the wrong models.py all along I want to thank Lain Shelvington for helping me solve this stupid issue.原来我一直在从错误的 models.py 导入我要感谢 Lain Shelvington 帮助我解决了这个愚蠢的问题。

Error says that, it is able to import the module models but couldn't find 'Idea' in that file.错误表明,它能够导入模块模型,但在该文件中找不到“Idea”。 Probably it is not present in the same folder.可能它不在同一个文件夹中。

Please check if 'Idea' is present in the same models.py or may be in your IDE you opened a different models.py and got confused.请检查“想法”是否存在于相同的 models.py 中,或者可能在您的 IDE 中,您打开了不同的 models.py 并感到困惑。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM