Django中的循环依赖错误

时间:2017-07-25 16:39:36

标签: python django

使用我的Django项目运行makemigrations / migrate时出现问题。

出现此错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\commands\migrate.py", line 139, in handle
    plan = executor.migration_plan(targets)
  File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 60, in migration_plan
    for migration in self.loader.graph.forwards_plan(target):
  File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\migrations\graph.py", line 149, in forwards_plan
    self.ensure_not_cyclic(target, lambda x: (parent.key for parent in self.node_map[x].parents))
  File "C:\Users\crstu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\migrations\graph.py", line 241, in ensure_not_cyclic
    raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
django.db.migrations.exceptions.CircularDependencyError: accounts.0001_initial, projects.0001_initial

我有两个应用程序 - 一个名为Accounts,另一个名为Projects

对于我的帐户应用,这是模型:

from django.contrib.auth.models import (
    AbstractBaseUser,
    BaseUserManager,
    PermissionsMixin
)
from django.db import models
from django.utils import timezone
from django.conf import settings
from django.db.models.signals import post_save
import os

from projects.models import Skill, Project


def avatar_upload_path(instance, filename):
    return os.path.join('avatars', 'user_{0}', '{1}').format(
        instance.user.id, filename)

class UserManager(BaseUserManager):
    def create_user(self, email, username=None, password=None):
        if not email:
            raise ValueError("Users must have an email address")

        if not username:
            username = email.split('@')[0]

        user = self.model(
            email=self.normalize_email(email),
            username=username,
        )
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, username, password):
        user = self.create_user(
            email,
            username,
            password,
        )
        user.is_staff = True
        user.is_superuser = True
        user.save()
        return user


class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=40, unique=True, default='')
    date_joined = models.DateTimeField(default=timezone.now)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    def __str__(self):
        return "@{}".format(self.username)

    def get_short_name(self):
        return self.username

    def get_long_name(self):
        return "@{} ({})".format(self.username, self.email)


class UserProfile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    first_name = models.CharField(max_length=40, default='', blank=True)
    last_name = models.CharField(max_length=40, default='', blank=True)
    bio = models.TextField(blank=True, default='')
    avatar = models.ImageField('Avatar picture',
                               upload_to=avatar_upload_path,
                               null=True,
                               blank=True)
    skills = models.ManyToManyField(Skill)

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

    @property
    def get_avatar_url(self):
        if self.avatar:
            return '/media/{}'.format(self.avatar)
        return 'http://www.gravatar.com/avatar/{}?s=128&d=identicon'.format(
            '94d093eda664addd6e450d7e9881bcad'
        )


def create_profile(sender, **kwargs):
    if kwargs['created']:
        user_profile = UserProfile.objects.create(user=kwargs['instance'])

post_save.connect(create_profile, sender=User)

我的项目应用程序就是模型。我想也许是因为我在另一个应用程序中使用了从UserProfile到Skill类的多对多字段,这可能就是问题...所以我试图在两个应用程序上分别运行makemigrations ---同时注释掉& #39;技术人员&#39;来自UserProfile。没有工作。所以不知道现在该做什么。

from django.conf import settings
from django.db import models

from django.core.urlresolvers import reverse

class Skill(models.Model):
    """User skills class."""
    ANDROID = 1
    DESIGNER = 2
    JAVA = 3
    PHP = 4
    PYTHON = 5
    RAILS = 6
    WORDPRESS = 7
    IOS = 8

    SKILL_CHOICES = (
        (str(ANDROID), 'Android Developer'),
        (str(DESIGNER), 'Designer'),
        (str(JAVA), 'Java Developer'),
        (str(PHP), 'PHP Developer'),
        (str(PYTHON), 'Python Developer'),
        (str(RAILS), 'Rails Developer'),
        (str(WORDPRESS), 'Wordpress Developer'),
        (str(IOS), 'iOS Developer')
    )

    name = models.CharField(max_length=140, choices=SKILL_CHOICES, default='unknown')

    def __str__(self):
        return self.get_name_display()


class Project(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='project', null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=255)
    description = models.TextField()
    requirements = models.TextField(default='')
    timeline = models.CharField(max_length=255, blank=True)
    complete = models.BooleanField(default=False)

    @property
    def open_positions(self):
        return self.positions.exclude(filled=True)

    def __str__(self):
        return self.title.title()

    def get_absolute_url(self):
        return reverse("projects:project_detail", kwargs={"pk": self.pk})


class Position(models.Model):
    project = models.ForeignKey(Project, default='',related_name='positions')
    name = models.CharField(max_length=140)
    description = models.TextField()
    skill = models.ForeignKey(Skill, default='', null=True)
    filled = models.BooleanField(default=False)

    def __str__(self):
        return '{} - {}'.format(self.project.title.title(), self.name.title())

2 个答案:

答案 0 :(得分:1)

当您遇到此类问题时,表示您正在相互导入类。您正在使用Foreignkey和ManytoMany字段中的每个其他app模型类。 因此,请使用以下方法在两个app的models.py中定义字段。

 field = models.ForeignKey('app_name.ModelClassName',)
 field = models.ManyToManyField('app_name.ModelClassName',)

答案 1 :(得分:1)

您的2次迁移相互依赖。查看为您的帐户应用生成的迁移,您可能会看到类似这样的内容

makemigrations

您的项目迁移中可能存在类似的内容

您需要删除这两个迁移文件,然后再次运行{"Message":"Authorization has been denied for this request."},而不指定应用名称