代码之家  ›  专栏  ›  技术社区  ›  E_K

根据模型中的用户应用迁移。py,而不是根据数据库中的迁移应用

  •  0
  • E_K  · 技术社区  · 6 年前

    我编写了一个定制的django migrations命令,如下所示

    User = get_user_model()
    
    def populate_asset_assignee(apps, schema_editor):
        for user in User.objects.all():
            user.save()
            # Some more code
    

    用户模型如下所示

    class User(AbstractUser):
        username = None
        email = models.EmailField(max_length=50, unique=True)
        cohort = models.IntegerField(blank=True, null=True)
        slack_handle = models.CharField(max_length=50,
                                    blank=True, null=True)
        picture = models.CharField(max_length=255, blank=True, null=True)
        phone_number = models.CharField(max_length=50, blank=True, null=True)
        last_modified = models.DateTimeField(auto_now=True, editable=False)
        password = models.CharField(max_length=128, blank=True, null=True)
        location = models.ForeignKey('SomeCentre',
                                blank=False,
                                null=True,
                                on_delete=models.PROTECT)
        # Some more fields
    

    我最近添加了location字段,并对其进行了迁移,这是在应用此自定义迁移之后应用的。我遇到的问题是,每当我尝试在 测试数据库 或者一个新的数据库,我得到了错误 django.db.utils.ProgrammingError: column core_user.location_id does not exist 这是在 populate_asset_assignee 方法当我尝试做 user in User.objects.all() 知道为什么吗 location_id 正在检查,但我还没有为位置字段应用迁移。

    0 回复  |  直到 6 年前