简体   繁体   中英

Django Python shell import problem [From app.models import Model] is not working

redcube/models.py

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


class Redcube(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField()
    price = models.IntegerField()
    created_date = models.DateTimeField()

    def __str__(self):
        return self.name


class Images(models.Model):
    redcube = models.ForeignKey(Redcube, on_delete=models.CASCADE, null=True)
    image = models.ImageField(upload_to='images/', blank=True, null=True)

    def __str__(self):
        return self.redcube.name


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    level = models.IntegerField(blank=True)

    def __str__(self):
        return self.user.username


class Basic(models.Model):
    basic = models.ManyToManyField(Redcube, through='Inventorytest', blank=True)


class Inventorytest(models.Model):
    redcube = models.ForeignKey(Redcube, on_delete=models.CASCADE, null=True)
    images = models.ForeignKey(Images, on_delete=models.CASCADE, null=True)
    basic = models.ForeignKey(Basic, on_delete=models.CASCADE, blank=True, null=True)

class testuser(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    level = models.IntegerField(blank=True)

    def __str__(self):
        return self.user.username

from the python shell

>>> from redcube.models import Profile
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: cannot import name 'Profile' from 'redcube.models' (/Users/macos/basic/btp/btp/redcube/models.py)
>>> from redcube.models import Images
>>> Images.objects.all()
<QuerySet [<Images: Hawaii>, <Images: City view hotel>, <Images: Cottage>, <Images: Redcube>, <Images: Phone>, <Images: Flower>, <Images: Miho>]>

When I try to import and test the models in the python shell Other model class is working right. but the Profile class is not working!

I've tried the from .models import Profile, but not working. Thanks for the help :)

I am using the pycharm.

I've used 2 terminals at the same time. When I add class on the models.py, I have done the makemigrations & migrate

But I did not exit the python shell.

When I exit the python shell after the migrate, and start the python shell agian.

It worked :)

thanks for the help guys~!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM