Question

I am trying to import a class from elements.models that i need as foreignkey on another class. Problem is, i get a long error list that i don't understand. Without the line, everything works just perfectly fine.

The model where i import the line

from django.db import models
from elements.models import Region

class Character(models.Model):
    """ User characters that hold the personal game stats """
    created = models.DateTimeField(auto_now_add=True)
    alive = models.BooleanField(default=True)
    name = models.CharField(max_length=63, unique=True)
    xp = models.IntegerField(default=0)
    region = models.ForeignKey(Region)
    alliance = models.ForeignKey('Alliance', null=True)
    credit = models.IntegerField(default=0)
    bullets = models.IntegerField(default=0)
    hitpoints = models.IntegerField()
    accuracy = models.FloatField(default=0)

    def __unicode__(self):
        return self.name

the elements.models Region class:

class Region(models.Model):
    """ type of booze and their base price """
    name = models.CharField(max_length=31, unique=True)
    alliance_slots = models.IntegerField()

    def __unicode__(self):
        return self.name

Now when I try to sync the database i get the following errorlist

http://pastebin.com/Y5kETg8b

any idea what causes this?

Was it helpful?

Solution

You may be having circular imports. You are importing the Region class in the models.py file containing your Character class while importing the Character class in the models.py file containing the Region class.

Try replacing your Character class with this

from django.db import models

class Character(models.Model):
""" User characters that hold the personal game stats """
created = models.DateTimeField(auto_now_add=True)
alive = models.BooleanField(default=True)
name = models.CharField(max_length=63, unique=True)
xp = models.IntegerField(default=0)
region = models.ForeignKey('elements.Region')
alliance = models.ForeignKey('Alliance', null=True)
credit = models.IntegerField(default=0)
bullets = models.IntegerField(default=0)
hitpoints = models.IntegerField()
accuracy = models.FloatField(default=0)

def __unicode__(self):
    return self.name
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top