Django-创建多个到多个模型的中间时出错

时间:2015-12-06 05:59:53

标签: python mysql django database-migration

我目前正在编写一个Django App,其中一项功能涉及用户能够向其他用户发送邀请,其中每个邀请都将存储在邀请数据库表中。为此,我遵循了docs以及此blog post中提供的结构。以下是用户和邀请(中间模型)的模型:

class User(models.Model):
    email = models.EmailField()
    full_name = models.CharField(max_length = 120, blank = True, null = True)
    timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
    updated = models.DateTimeField(auto_now_add = False, auto_now = True)
    invites = models.ManyToManyField('self', through = 'Invite', 
        symmetrical = False )

    def __unicode__(self):
        return self.full_name

class Invite(models.Model):
    sender_id = models.Foreignkey(User, on_delete = models.CASCADE )
    recipient_id = models.Foreignkey(User,on_delete = models.CASCADE)
    concert_id = models.AutoField()
    artist_id = models.AutoField()
    message = models.CharField(max_length = 120, blank = True, null = True)
    date_sent = models.DateTimeField(auto_now_add = True, auto_now = False)

出于某种原因,当我尝试运行数据库迁移时,我收到一个属性错误,指出模块对象没有属性Foreignkey,引用了sender_id定义的行。基于docs中提供的代码,models对象确实具有Foreignkey属性,那么可能是什么问题呢?

1 个答案:

答案 0 :(得分:3)

问题是ForeignKey是用大写K写的,如:

sender_id = models.ForeignKey(User, on_delete = models.CASCADE)

这应该解决这个问题,但你可能还需要在一个或两个外键中添加一个related_name属性,因为它们都指向同一个模型(Why cannot use 2 separate foreign keys for same model in django model