Django很多很多,其他字段不起作用

时间:2017-08-01 09:57:57

标签: python django

我是Django的新手,并跟着this编写了我的models.py文件:

class Coin(models.Model):
    name = models.CharField(max_length=50, unique=True)

    class Meta:
        verbose_name = 'coin'
        verbose_name_plural = 'coins'
        ordering = ['name']

    def __str__(self):
        return self.name


class PublicPortfolioManager(models.Manager):
    def get_queryset(self):
        qs = super(PublicPortfolioManager, self).get_queryset()
        return qs.filter(is_public=True)


class Portfolio(models.Model):
    title = models.CharField('title', max_length=255)
    date_created = models.DateTimeField('date created')
    date_updated = models.DateTimeField('date updated')
    is_public = models.BooleanField('public', default=True)
    owner = models.ForeignKey(User, verbose_name='owner',
        related_name='portfolio')

    coins = models.ManyToManyField(Coin, through='CoinInfo')
    objects = models.Manager()
    public = PublicPortfolioManager()

    def __str__(self):
        return '%s' % (self.title)

def save(self, *args, **kwargs):
    if not self.id:
        self.date_created = now()
    self.date_updated = now()
    super(Portfolio, self).save(*args, **kwargs)

class CoinInfo(models.Model):
    coin = models.ForeignKey(Coin)
    portfolio = models.ForeignKey(Portfolio)
    amount = models.FloatField(default=0.0)
    #date_purchased = models.DateTimeField(auto_created=True)

除非我想保存CoinInfo的实例,否则一切都会正常工作,然后我收到以下错误:表hello_coininfo没有名为coin_id的列。在创建实例之前,我已经刷新数据库并运行makemigrations并进行迁移。我做错了什么?

1 个答案:

答案 0 :(得分:0)

应用迁移时出现问题,导致不在db中创建coin.id字段,因此出现此问题。