Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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}"

Python Shell

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use python manage.py shell. In this way it automatically imports settings that are important for django related modules import.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading