why can't I import my models in Python shell?
I don't even know what this is. Works fine when I runserver
. But I get an Error when trying to import in the Python Shell.
My app is called auctions
and my models.py
(using AbstractUser
) file is:
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}"
Use python manage.py shell
. In this way it automatically imports settings that are important for django related modules import.