本地字段与类似名称的字段发生冲突

时间:2015-02-22 20:13:11

标签: python django django-models

我试图添加一个新的数据库模型,让我能够"分组"费用,但在运行python manage.py 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

&#39;

这是模型代码 - 我尝试将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)

3 个答案:

答案 0 :(得分:1)

你可以发布TimeStampedModel定义吗?我怀疑你没有将基本模型声明为Abstract。这就是“id”字段与彼此冲突的原因。

class TimeStampedModel(models.Model):

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

    class Meta:
        abstract = True

答案 1 :(得分:1)

这是Django 1.7.3的一个错误,升级到最新版本,你会没事的(当时是1.7.5)

答案 2 :(得分:0)

这比你发布的更多。你可以发布整个模型文件吗?或者一个要点的链接?我很惊讶你没有从'ExpenseGroup'模型中获得关于FK引用'Expense'的错误,因为它在文件的后面定义。

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