簡體   English   中英

Python 無法導入名稱 Django

[英]Python Cannot Import Name Django

我收到以下錯誤

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

模型.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)

序列化程序.py:

from rest_framework import serializers
from .models import Idea

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

我不知道為什么這不起作用,我以前在許多項目中都以相同的方式導入過。

這是目錄: 在此處輸入圖像描述

原來我一直在從錯誤的 models.py 導入我要感謝 Lain Shelvington 幫助我解決了這個愚蠢的問題。

錯誤表明,它能夠導入模塊模型,但在該文件中找不到“Idea”。 可能它不在同一個文件夾中。

請檢查“想法”是否存在於相同的 models.py 中,或者可能在您的 IDE 中,您打開了不同的 models.py 並感到困惑。

暫無
暫無

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

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