如何追踪这个? AttributeError:'NoneType'对象在makemigrations期间没有属性'is_relation'

时间:2017-05-03 16:43:58

标签: python django django-models django-migrations

我从昨天开始第二次遇到一个令人困惑的错误。上次我只是把我的整个迁移弄平了,但我从来没有真正找到导致问题的原因。

所以当我尝试为我的python项目制作游戏时会出现这种情况。我应该在哪里寻找错误?我觉得它实际上并不是关于迁移,而是关于views.py或models.py中的错误,即使我完全不明白为什么这会影响数据库迁移。

无论如何,这些错误都没有指向我写过的代码。这一切都在Django。那么如何找到导致该错误的错误呢?

(testenv1) C:\Users\user\eclipse_workspace\test1\test1>python manage.py makemigrations --trace
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\testenv1\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "C:\Python27\testenv1\lib\site-packages\django\core\management\__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\testenv1\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\testenv1\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\Python27\testenv1\lib\site-packages\django\core\management\commands\makemigrations.py", line 150, in handle
    loader.project_state(),
  File "C:\Python27\testenv1\lib\site-packages\django\db\migrations\loader.py", line 323, in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
  File "C:\Python27\testenv1\lib\site-packages\django\db\migrations\graph.py", line 409, in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)
  File "C:\Python27\testenv1\lib\site-packages\django\db\migrations\migration.py", line 92, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "C:\Python27\testenv1\lib\site-packages\django\db\migrations\operations\fields.py", line 148, in state_forwards
    delay = not old_field.is_relation
AttributeError: 'NoneType' object has no attribute 'is_relation'

(testenv1) C:\Users\user\eclipse_workspace\test1\test1>

这是我的models.py。它目前是WIP:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
#from wheel.metadata import unique
from datetime import datetime
from django.urls import reverse
# Create your models here.
from django.shortcuts import redirect

class Task(models.Model):
    name = models.CharField(max_length=255,blank=True)
    code = models.SlugField(max_length=255, unique=True)
    sender = models.ForeignKey("Sender", default=0)
    client = models.ForeignKey("Client", null=True,blank=True)
    datetime = models.DateTimeField(default=datetime.now)
    duration = models.IntegerField(default=0,blank=True)
    photo = models.ImageField(null=True, blank=True)
    route = models.TextField(blank=True)
    km = models.FloatField(default=0, blank=True)
    notes = models.TextField(blank=True)
    milage_receipt = models.ForeignKey("MilageReceipt", null=True, blank=True, on_delete=models.SET_NULL)

    def date(self):
        return self.datetime.date()
    def status(self):
        try: 
            if self.receipt and self.milage_receipt:
                return "done"
            elif not self.receipt:
                return "new"
            elif not self.milage_receipt:
                return "ok"
        except Receipt.DoesNotExist:
            return "new"
        except MilageReceipt.DoesNotExist:
            return "processing"
        return str(self.receipt)

    def save(self, *args, **kwargs):
        if self.duration == None:
            self.duration = 0
        if self.km == None:
            self.km = 0
        super(Task, self).save(*args, **kwargs) # Call the "real" save() method.

    def get_absolute_url(self):
        return reverse('task_update', args=[str(self.id)])

    def __unicode__(self):
            return self.code


class Client(models.Model):
    name = models.CharField(max_length=255)
    name2 = models.CharField(max_length=255, blank=True)
    slug = models.SlugField(unique=True, max_length=255)
    email = models.EmailField()
    handle = models.CharField(max_length=255, blank=True)
    contact_name = models.CharField(max_length=255, blank=True)
    street = models.CharField(max_length=255)
    zip = models.IntegerField()
    city = models.CharField(max_length=255)
    street2 = models.CharField(max_length=255,blank=True)
    zip2 = models.CharField(max_length=255,blank=True)
    city2 = models.CharField(max_length=255,blank=True)
    default_vat = models.IntegerField(default=7)
    km_price = models.FloatField(default=0)
    active = models.BooleanField(default=False)
    def __unicode__(self):
            return self.slug

class Sender(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField(max_length=255)
    email = models.EmailField()
    street = models.CharField(max_length=255)
    zip = models.IntegerField()
    city = models.CharField(max_length=255)
    phone = models.CharField(max_length=255)
    website = models.URLField()
    tax_id = models.CharField(max_length=255)
    vat_id = models.CharField(max_length=255)
    bank = models.ForeignKey("BankAccount")

    def __unicode__(self):
        return self.slug

class BankAccount(models.Model):
    name = models.CharField(max_length=255)
    iban = models.CharField(max_length=255)
    bic = models.CharField(max_length=255)
    holder = models.CharField(max_length=255)

class ReceiptNumber(models.Model):
    id = models.AutoField(primary_key=True, unique=True)
    number = models.IntegerField(blank=True)

    def __unicode__(self):
        return str(self.id)

class MyQuerySet(models.query.QuerySet):
    def delete(self):
        print self
        raise

class SingleDeleteManager(models.Manager):
    def get_query_set(self):
        return MyQuerySet(self.model, using=self._db)  

class Receipt(models.Model):
    number = models.OneToOneField("ReceiptNumber", blank=True, related_name="receipt", null=True)
    vat = models.IntegerField(blank=True)
    amount = models.DecimalField(max_digits=10, decimal_places=2, blank=True)
    body = models.TextField(blank=True)
    date = models.DateField(default=datetime.now, blank=True)
    task = models.OneToOneField("Task", related_name="receipt")
    objects = SingleDeleteManager()
    def total(self):
        a = float(self.amount) * (self.vat / 100.0) + float(self.amount)
        return "%.2f" % round(a,2)
    def vat_amount(self):
        a = float(self.amount) * (self.vat / 100.0)
        return "%.2f" % round(a,2)

    def save(self,*args, **kwargs):
        if self.date == None:
            self.date = datetime.now()
        if self.amount == None:
            self.amount = 0  
        if self.vat == None:
            self.vat = self.task.client.default_vat
        super(Receipt, self).save(*args, **kwargs)
        #if not hasattr(self, "number") or self.number == None:
        #    new_number = ReceiptNumber.objects.create()
        #    new_number.number = ReceiptNumber.objects.filter(Q(receipt__task__sender_id=self.sender.id) | Q(milage_receipt__task__sender_id=self.sender.id)).latest('id').number+1
        #    new_number.save()
        #    self.number = new_number 
        #    super(Receipt, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        if hasattr(self,"number") and self.number.id:
            self.number.delete()
        return super(self.__class__, self).delete(*args, **kwargs)
    def get_absolute_url(self):
        return reverse('receipt_create_for_task', args=[str(self.task.id)])
    def __unicode__(self):
        try:
            return str(self.number) + ": " + self.task.code
        except Task.DoesNotExist:
            return "should be gone"

class ReceiptTemplate(models.Model):
    name = models.CharField(max_length=255)
    vat = models.IntegerField()
    amount = models.FloatField()
    body = models.TextField()

class MilageReceipt(models.Model):
    number = models.OneToOneField("ReceiptNumber", null=True)
    sender = models.ForeignKey("Sender")
    client = models.ForeignKey("Client")

版本1.11!

5 个答案:

答案 0 :(得分:7)

我发现Radek在this ticket on Django上进行调试的一种非常有用的方法。

如果您觉得自己很勇敢,可以在state_forwards()上编辑django/db/migrations/operations/fields.py方法,在第一行添加调试打印。

def state_forwards(self, app_label, state):
    # THIS IS THE LINE TO ADD
    print app_label + " " + self.model_name_lower + " " + self.name
    # END OF THE LINE TO ADD
    new_fields = []
    old_field = None
    for name, instance in state.models[app_label, self.model_name_lower].fields:
        if name != self.name:
            new_fields.append((name, instance))
        else:
            old_field = instance
    state.models[app_label, self.model_name_lower].fields = new_fields
    # Delay rendering of relationships if it's not a relational field
    delay = not old_field.is_relation
    state.reload_model(app_label, self.model_name_lower, delay=delay)

然后再次运行./manage.py makemigrations。在崩溃之前,您应该看到使迁移失败的模型的名称。

在以下示例中,请注意,崩溃前要处理的最后一个模型是adsummary应用程序上的core,更具体地说是account字段。顺便说一句,找到此内容后,我只是在adsummary应用程序的migrations文件夹中搜索了core,然后发现在一个迁移中有一个删除此列的命令文件,尽管该列已不在数据库中,但删除了此命令,makemigrations重新开始工作。

Felipes-MacBook-Air:backend felipe$ ./manage.py makemigrations -v 3
contenttypes contenttype name
core adsummary account
Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 150, in handle
    loader.project_state(),
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/loader.py", line 323, in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/graph.py", line 409, in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/migration.py", line 92, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 149, in state_forwards
    delay = not old_field.is_relation
AttributeError: 'NoneType' object has no attribute 'is_relation'

答案 1 :(得分:2)

同样的迁移错误发生在我身上。在我的情况下,它是由底层的MySQL表模式与models.py中的模型规范不同步引起的。具体来说,我的表在早期失败的迁移过程中留下了一列。

我通过手动删除MySQL中的列并确保表模式与models.py匹配来解决问题。我删除了导致错误的失败迁移,然后重新重新执行了已删除的迁移。因为在运行迁移之前,基础表和models.py模式现在处于同步状态,所以它顺利进行。

我希望这有帮助!

答案 2 :(得分:2)

同样的事发生在我身上。我认为Django 1.11中存在错误。我降级到Django 1.10.7并且没有问题地运行我的迁移并在此之后再次升级到1.11。 这里有一张开放票:https://code.djangoproject.com/ticket/28073

答案 3 :(得分:1)

正如FamousJameous所说,问题出在RemoveField方法上,可能用于迁移。就我而言,问题出在我尝试删除未定义的字段时(例如:我尝试删除由关系创建的field_id,但定义正确,正确的是{{1 }}。

答案 4 :(得分:0)

我不确定100%如何解决此问题,但是我会写出对我有用的东西:

在以下路径中找到函数state_forwards(我正在使用ubuntu):

"/home/aseem/venv/bt_pip/lib/python3.6/site-packages/django/db/migrations/operations/fields.py"

bt_pip是我的pip虚拟环境名称,存储在文件夹venv中。在state_forwards函数上方,添加以下行:

print (app_label + " " + self.model_name_lower + " " + self.name + str(self.field))

这将显示引起问题的应用程序。

现在安装django-reset-migrations。然后在终端运行:

python manage.py reset_migrations app_name

它向我显示了一些错误,但是之后我尝试使用python manage.py makemigrations时就起作用了。