ImportError: 无法从’EmployeeApp.models’导入名称’Employees’

huangapple go评论66阅读模式
英文:

ImportError: cannot import name 'Employees' from 'EmployeeApp.models'

问题

我对使用Django框架、Python和Flask构建应用程序还相当新手。我尝试导入一些模块并收到了以下错误。有人能帮忙吗?

我的代码如下:

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from django.http.response import JsonResponse

from EmployeeApp.models import Departments, Employees
from EmployeeApp.serializers import DepartmentSerializer, EmployeeSerializer

我看到的错误信息如下:

ImportError: 无法从 'EmployeeApp.models' 导入名称 'Employees'(来自 /文件路径)

rest_framework.parsers 无法解析。另外,在第6行的 Employees 和第7行的 EmployeeSerializer 似乎无法导入。

如果您需要更多信息,请告诉我。

我期望运行 python manage.py runserver 成功,但由于上述错误而让我感到担忧。

Models.py

class Employees(models.Model):
    EmployeeId = models.AutoField(primary_key=True)
    EmployeeName = models.CharField(max_length=500)
    Department = models.CharField(max_length=500)
    DateOfJoining = models.DateField()
    PhotoFileName = models.CharField(max_length=500)
英文:

I am pretty new to Building an application with Django Framework, Python and Flask. I tried to import reference some module and i receive this error. Can someone help?
My code is below

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from django.http.response import JsonResponse

from EmployeeApp.models import Departments,Employees
from EmployeeApp.serializers import DepartmentSerializer,EmployeeSerializer

The error that i saw

ImportError: cannot import name 'Employees' from 'EmployeeApp.models'(/path/to/the/file.py)

rest_framework.parsers could not be resolved. Also, it seem that the line of the Employees in line 6 & EmployeeSerializer in line 7 are not able to be imported.

Please let me know if you need further information.

I expected running python manage.py runserver to pass successfully but with the failed error above, it gives me worries.

Models.py

class Employees(models.Model):
EmployeeId = models.AutoField(primary_key=True)
EmployeeName = models.CharField(max_length=500)
Department = models.CharField(max_length=500)
DateOfJoining = models.DateField()
PhotoFileName = models.CharField(max_length=500)

答案1

得分: 0

从相同的应用程序目录中导入这些导入项:

from .models import Departments, Employees
from .serializers import DepartmentSerializer, EmployeeSerializer
英文:

Try this imports if you are in same app directory

from .models import Departments,Employees
from .serializers import DepartmentSerializer,EmployeeSerializer

huangapple
  • 本文由 发表于 2023年7月11日 09:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定