为什么我不能在 Python shell 中导入我的模型?
why can't I import my models in Python shell?
我什至不知道这是什么。当我 runserver
时工作正常。但是在 Python Shell.
中尝试导入时出现错误
我的应用名为 auctions
,我的 models.py
(使用 AbstractUser
)文件是:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64, blank=True)
image = models.ImageField(upload_to="portraits", blank=True, default="default.jpg")
def __str__(self):
return f"{self.username}"
class Category(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64)
description = models.CharField(max_length=255, blank=True)
def __str__(self):
return f"{self.name}"
class Item(models.Model):
id = models.AutoField(primary_key=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="inventory")
name = models.CharField(max_length=64)
description = models.CharField(max_length=255)
image = models.ImageField(blank=True, default="Megamind.jpg")
starting_bid = models.PositiveIntegerField(default=0)
category = models.ForeignKey(Category, on_delete=models.CASCADE, default="1", related_name="stuff")
active = models.BooleanField(default=True)
favorited = models.ManyToManyField(User, blank=True, related_name="favorites")
def __str__(self):
return f"{self.name} of {self.owner}"
使用python manage.py shell
。通过这种方式,它会自动导入对 django 相关模块导入很重要的设置。
我什至不知道这是什么。当我 runserver
时工作正常。但是在 Python Shell.
我的应用名为 auctions
,我的 models.py
(使用 AbstractUser
)文件是:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64, blank=True)
image = models.ImageField(upload_to="portraits", blank=True, default="default.jpg")
def __str__(self):
return f"{self.username}"
class Category(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64)
description = models.CharField(max_length=255, blank=True)
def __str__(self):
return f"{self.name}"
class Item(models.Model):
id = models.AutoField(primary_key=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="inventory")
name = models.CharField(max_length=64)
description = models.CharField(max_length=255)
image = models.ImageField(blank=True, default="Megamind.jpg")
starting_bid = models.PositiveIntegerField(default=0)
category = models.ForeignKey(Category, on_delete=models.CASCADE, default="1", related_name="stuff")
active = models.BooleanField(default=True)
favorited = models.ManyToManyField(User, blank=True, related_name="favorites")
def __str__(self):
return f"{self.name} of {self.owner}"
使用python manage.py shell
。通过这种方式,它会自动导入对 django 相关模块导入很重要的设置。