Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

python - Local field clashes with field of similar name

I'm trying to add a new database model that will let me "group" expenses, but am running across this issue when running python manage.py makemigrations

My virtual environment looks like this:

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

Here is the traceback:

    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

'

This is the model code - I've tried cutting the ExpenseGroup model down to only the groupName as a field but I get the same error. What am I missing?

    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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can you post the TimeStampedModel definition? I suspect that you didn't declare the base model as Abstract. That why the "id" fields are conflicted with each others.

class TimeStampedModel(models.Model):

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

    class Meta:
        abstract = True

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...