本地字段与同名字段冲突

Local field clashes with field of similar name

我正在尝试添加一个新的数据库模型,它将让我 "group" 支出,但是 运行 在 运行 python [=24] 时遇到了这个问题=] makemigrations

我的虚拟环境是这样的:

Django==1.7.3
argparse==1.2.1
django-braces==1.4.0
django-chartit==0.1
django-crispy-forms==1.4.0
django-debug-toolbar==1.2.2
psycopg2==2.6
six==1.9.0
sqlparse==0.1.14
wsgiref==0.1.2

这是回溯:

    Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 111, in handle
    convert_apps=app_labels or None,
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 42, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 109, in _detect_changes
    self.old_apps = self.from_state.render(ignore_swappable=True)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 67, in render
    model.render(self.apps)
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 316, in render
    body,
  File "/home/jeff/.virtualenvs/ggc/local/lib/python2.7/site-packages/django/db/models/base.py", line 229, in __new__
    'base class %r' % (field.name, name, base.__name__)
django.core.exceptions.FieldError: Local field u'id' in class 'ExpenseGroup' clashes with field of similar name from base class 'Asset

'

这是模型代码 - 我尝试将 ExpenseGroup 模型缩减为仅将 groupName 作为字段,但我遇到了同样的错误。我错过了什么?

    class Asset(TimeStampedModel):
        assetName    = models.CharField(max_length=255)
        assetAddress = models.CharField(max_length=255)
        slug         = models.SlugField(max_length=255, blank=True)

        class Meta:
            unique_together = ('assetName', 'assetAddress')

        def __str__(self):
            return self.assetName

        def save(self, *args, **kwargs):
            self.slug = slugify(self.assetName)
            super(Asset, self).save(*args, **kwargs)


class ExpenseGroup(TimeStampedModel):
    groupName          = models.CharField(max_length=255, blank=False)
    expenseName        = models.ForeignKey(Expense, related_name='expenseGroup')

    class Meta:
        unique_together = ('expenseName', 'groupName')

    def __str__(self):
        return self.groupName

    def save(self, *args, **kwargs):
        return super(ExpenseGroup, self).save(*args, **kwargs)



class Expense(TimeStampedModel):
    assetName          = models.ForeignKey(Asset, related_name='assetExpense')
    category           = models.ForeignKey(ExpenseCategory, related_name='expenseCategory')
    expensePeriod      = models.DateTimeField(blank=False)
    expenseName        = models.CharField(max_length=255, blank=False)
    expenseAmount      = models.DecimalField(max_digits=20, decimal_places=2, blank=True)

    class Meta:
        unique_together = ('expenseName', 'expensePeriod')

    def __str__(self):
        return self.expenseName

    def save(self, *args, **kwargs):
        super(Expense, self).save(*args, **kwargs)

你能post TimeStampedModel 定义吗?我怀疑您没有将基本模型声明为抽象模型。这就是 "id" 字段相互冲突的原因。

class TimeStampedModel(models.Model):

    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

这是Django 1.7.3的一个bug,升级到最新的就可以了(当时是1.7.5)

这比您post编辑的要多。你能 post 你的整个模型文件吗?或 link 要点?我很惊讶你没有收到关于 'ExpenseGroup' 模型中对 'Expense' 的 FK 引用的错误,因为它在文件的后面定义。

你有任何现有的迁移吗?我建议删除任何现有的迁移和所有 pyc 文件,然后重试。