55

I got an error I don't understand !

cannot import name Item

In my model, I have items. These items are required for actions. But some of these items have an effect on actions :

items

from django.db import models
from effects.models import Effect

class Type(models.Model):
    name = models.CharField(max_length=200)

    def __unicode__(self):
         return self.name

class Item(models.Model):
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    type = models.ForeignKey(Type)
    quality = models.IntegerField()
    effects = models.ManyToManyField(Effect,through='ItemEffect',blank=True)
    item_requirement = models.ManyToManyField('self',through='ItemCraft',symmetrical=False,blank=True)
points = models.IntegerField()

    def __unicode__(self):
        return self.name

class Food(Item):
    ap = models.IntegerField()

class Tool(Item):
    durability = models.IntegerField()

[....]

class ItemEffect(models.Model):
    item = models.ForeignKey(Item)
    effect = models.ForeignKey(Effect)

def __unicode__(self):
    return self.item.name+':'+str.lower(self.effect.name)

class Meta:
    verbose_name_plural = 'items effects'

class ItemCraft(models.Model):
    item = models.ForeignKey(Item,related_name='%(class)s_item_crafted')
    item_requirement = models.ForeignKey(Item,related_name='%(class)s_item_required')
    number = models.IntegerField()

    def __unicode__(self):
        return self.item.name+' requires '+str.lower(self.item.name)+'('+self.number+')'

    class Meta:
        verbose_name_plural = 'items crafts'

actions

from django.db import models
from items.models import Item

class Action(models.Model):
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    pa = models.IntegerField()

    def __unicode__(self):
        return self.name

class CraftAction(Action):
    item = models.ForeignKey(Item)

    def __unicode__(self):
        return self.item.name+'\'s craft'

    class Meta:
        verbose_name_plural = 'crafts actions'

effects

from django.db import models
from actions.models import Action

class Effect(models.Model):
    action = models.ForeignKey

class ApEffect(Effect):
    ap = models.IntegerField()
Cyril F
  • 1,247
  • 3
  • 16
  • 31
  • Is the name of the "app" folder that contains these models called "items"? – imm Oct 07 '11 at 07:50
  • @pppery circular imports of __Django models__ is a separate issue. This question has a Django-specific answer. This is not a duplicate. – Max Malysh Aug 20 '19 at 22:59

5 Answers5

103

There is a circular import in your code, that's why the Item can't be imported in action.

You can solve the problem by removing the import of a class in one of your files, and replacing it with a string containing the name of the class, as explained in the documentation. For example :

effects = models.ManyToManyField('effects.Effect',through='ItemEffect',blank=True)
madjar
  • 12,691
  • 2
  • 44
  • 52
31

Like madjar suggested, there is likely a circular import in your code. If you're having trouble finding out where the circle is (which modules and imports are involved), you can use the traceback option to get an idea of where the problem lies:

python manage.py validate --traceback

Edit - Validate is deprecated from django 1.7. So please run the following command -

python manage.py check --traceback
Arvind Kumar
  • 913
  • 10
  • 23
Divisible by Zero
  • 2,616
  • 28
  • 31
  • This solved the issue for me. I was getting the same "Cannot import name Item" error. In my case however, I had a model called Item defined in my models.py file. I removed those models and started over with new definitions. Doing a `manage.py sqlclear app` did not solve it. Deleting the .pyc files and restarting the server fixed it. – frishi Jun 18 '13 at 20:35
  • 1
    Worth looking at what .pyc files are: http://stackoverflow.com/questions/8822335/what-the-different-python-file-extensions-stand-for – frishi Jun 18 '13 at 20:42
10

This was the first post that came up on Google so I will post this alternative cause of error.

In my code there was no circular imports, I solved this problem by manually deleting all .pyc files in my project. Apparently restarting the application wasn't recompiling my code.

Pythonator
  • 191
  • 2
  • 3
10

Try to import Locally your model instead of as public one, Example

def sample_function():
    from effects.models import Effect # import within function or class

or import model as String -> 'APP_NAME.MODEL_NAME'

pay_methods = models.ManyToManyField('payment_app.AllowedPayMethod')
ugali soft
  • 2,719
  • 28
  • 25
0

Similar situation to Pythonator - I had an alternate cause for the same error message.

In my case, I had forgotten to activate the virtual environment I set up for my project and was trying to run the server. After activating the environment and running the server again I had no issues.

Jonathan Porter
  • 1,365
  • 7
  • 34
  • 62