python 类名未定义

问题描述 投票:0回答:1

im new to this language

this is my model.py where the class are declared

from django.db import models
from django.contrib.auth.models import AbstractUser

Create your models here.

class CustomUser(AbstractUser):
USER = (
('1','HOD'),
('2','STAFF'),
('3','STUDENT'),
)

user_type = models.CharField(choices=USER,max_length=50,default=1)
profile_pic = models.ImageField(upload_to='media/profile_pic')

# Create Course and session Model

class Course( models.Model ):
    name = models.CharField( max_length=100 )
    created_at = models.DateTimeField( auto_now_add=True )
    updated_at = models.DateTimeField( auto_now=True )

    def __str__(self):
        return self.name

class Session_Year( models.Model ):
    session_start = models.CharField( max_length=100 )
    session_end = models.CharField( max_length=100 )

    def __str__(self):
        return self.session_start + " To " + self.session_end

then i makemigrations and migrate it

and when i call it in admin,py

from django.contrib import admin
from .models import *
from django.contrib.auth.admin import UserAdmin

Register your models here.

class UserModel(UserAdmin):
list_display = ['username','user_type']

admin.site.register(CustomUser,UserModel)
admin.site.register(Course)
admin.site.register(Session_Year)

this happen
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\PC\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1052, in _bootstrap_inner
self.run()
File "C:\Users\PC\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 989, in run
self.
target(*self.args, **self.kwargs)
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
autoreload.raise_last_exception()
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
raise exception[1]
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django\core\management_init
.py", line 394, in execute
autoreload.check_errors(django.setup)()
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django_
init
.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django\apps\registry.py", line 124, in populate
app_config.ready()
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django\contrib\admin\apps.py", line 27, in ready
self.module.autodiscover()
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django\contrib\admin_
init
.py", line 50, in autodiscover
autodiscover_modules("admin", register_to=site)
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\venv\Lib\site-packages\django\utils\module_loading.py", line 58, in autodiscover_modules
import_module("%s.%s" % (app_config.name, module_to_search))
File "C:\Users\PC\AppData\Local\Programs\Python\Python312\Lib\importlib_
init
.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1381, in _gcd_import
File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 929, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 994, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "C:\Users\PC\PycharmProjects\Yt-Student-Management-System\student_management_system\app\admin.py", line 13, in <module>
admin.site.register(Course)
^^^^^^
NameError: name 'Course' is not defined

the Course is not define

please help meee!

i try deleting the migrations and remigrate them and retyping but still no avail i really dont know

python python-3.x youtube
1个回答
0
投票

您似乎没有在 admin.py 中导入课程模型 使用前必须导入才能使用。

例如

# course/models.py
class Course(models.Model):
...

# app/admin.py
from course.models import Course

...

admin.site.register(Course)
© www.soinside.com 2019 - 2024. All rights reserved.