python ImportError:cannot import name 'Students' from 'django.contrib.auth.models' Django

odopli94  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(183)

我目前正在尝试python Django版本4.2.2来为一个活动做一个项目,我想问一下问题是什么,因为每当我在VSCode终端中运行我的项目文件时,我都会得到以下错误

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Python39\lib\threading.py", line 950, in _bootstrap_inner
    self.run()
  File "C:\Python39\lib\threading.py", line 888, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Python39\lib\site-packages\django\core\management\commands\runserver.py", line 133, in inner_run
    self.check(display_num_errors=True)
  File "C:\Python39\lib\site-packages\django\core\management\base.py", line 485, in check
    all_issues = checks.run_checks(
  File "C:\Python39\lib\site-packages\django\core\checks\registry.py", line 88, in 
run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Python39\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "C:\Python39\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
    return check_method()
  File "C:\Python39\lib\site-packages\django\urls\resolvers.py", line 494, in check    for pattern in self.url_patterns:
  File "C:\Python39\lib\site-packages\django\utils\functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python39\lib\site-packages\django\urls\resolvers.py", line 715, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Python39\lib\site-packages\django\utils\functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python39\lib\site-packages\django\urls\resolvers.py", line 708, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked       
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed     
  File "C:\Users\admin\Desktop\runapp\amsdjangocrud\urls.py", line 23, in <module>
    path('', include('amsapps.urls')),
  File "C:\Python39\lib\site-packages\django\urls\conf.py", line 38, in include    
    urlconf_module = import_module(urlconf_module)
  File "C:\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked       
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed     
  File "C:\Users\admin\Desktop\runapp\amsapps\urls.py", line 2, in <module>
    from . import views
  File "C:\Users\admin\Desktop\runapp\amsapps\views.py", line 4, in <module>       
    from django.contrib.auth.models import Students
ImportError: cannot import name 'Students' from 'django.contrib.auth.models' (C:\Python39\lib\site-packages\django\contrib\auth\models.py)

这是我的models.py

from django.forms import ModelForm, Textarea
from django.db import models

class Students(models.Model):
    student_id = models.IntegerField(primary_key=True)
    id_number = models.IntegerField(blank=True, null=True)
    firstname = models.CharField(max_length=50)
    lastname = models.CharField(max_length=50)
    gender = models.CharField(max_length=50)
    address = models.CharField(max_length=200)
    contact = models.IntegerField(blank=True, null=True)
    course = models.CharField(max_length=50)

    def __str__(self):
        return f'{self.course}, {self.contact}, {self.address}, {self.gender}, {self.lastname}, {self.firstname}, {self.id_number}, {self.student_id},'

class Assignment(models.Model):
    assignment_id = models.IntegerField(primary_key=True)
    assignment_name = models.CharField(max_length=50)
    instructor = models.ForeignKey('Instructor',on_delete=models.CASCADE)
    status = models.ForeignKey('Status',on_delete=models.CASCADE)
    subject = models.ForeignKey('Subject',on_delete=models.CASCADE)

    
    class Meta:
        managed = True
        db_table = 'assignment'

class Instructor(models.Model):
    instructor_id = models.IntegerField(primary_key=True)
    firstname = models.CharField(max_length=50)
    lastname = models.CharField(max_length=50)

    class Meta:
        managed = True
        db_table = 'instructor'

下面是我的导入和我的www.example.com上的函数views.py

from django.contrib.auth.models import Students
from django.contrib.auth.models import Assignment

def main(request, id):
    joined_data = Assignment.objects.get(id>=1)
    return render(request, 'amsapp/main.html', {'data': joined_data })

我很困惑我似乎哪里出错了,任何帮助将不胜感激
我尝试在www.example.com中插入模型admin.py,格式如下:

from .models import (
    Assignment,
    Instructor,
    Student,
    Subject
)

但问题依然存在

ajsxfq5m

ajsxfq5m1#

ImportError:无法从“django.contrib.auth.models”导入名称“Students”
这已经告诉了你代码中哪些地方不起作用。你从django.<...>导入的所有东西都是从官方的django包导入的。这意味着代码不是你写的,而是通过你的包管理器安装的,最初是由django团队编写的。
不过,您希望导入“自己的”模型。如果你在同一个应用程序中导入,你可以编写和你在www.example.com中所做的完全相同的导入语句admin.py。只需将import语句从admin.py复制并粘贴到views.py。

from .models import Assignment, Student

如果你需要从另一个应用程序导入一些东西,请使用以下语法:

from <appname>.models import <modelname>
from shop.models import Product

**注意:**如果你想使用djangos认证系统(我提到这是因为这是django.contrib.auth中的代码),你可能应该考虑从AbstractUser继承你的Student模型。也许这就是你最初的目标。

from django.contrib.auth.models import AbstractUser
class Students(AbstractUser):

这将给予您的Student提供来自djangos内置身份验证系统的所有字段和功能。

相关问题