PostgreSQL 的 Django 错误

Django error with PostgreSQL

我试图 运行 我的 Django 应用程序使用 PostgreSQL 作为我的数据库引擎。部署后,我从模板加载器收到模板呈现错误。它适用于我使用 ModelChoiceField 创建的表单。

而且在迁移时我收到一条错误消息

django.db.utils.ProgrammingError: column "category_parent" cannot be cast automatically to type integer HINT: You might need to specify "USING category_parent::integer".

我认为从 select 字段返回索引是导致问题的原因。是否可以毫不费力地解决这个问题。

这是该表格的 class。

class CategoryForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
    super(CategoryForm, self).__init__(*args, **kwargs)
    self.fields['category_parent'].required = False

category_parent = forms.ModelChoiceField(
    queryset=Category.objects.all(), empty_label='None')
category_image = forms.ImageField()

class Meta:
    model = Category
    fields = ('category_name', 'category_code',)

我收到的模板错误的屏幕截图。

我的模型class相同

class Category(models.Model):
    category_name = models.CharField(max_length=300)
    category_code = models.CharField(max_length=100)
    category_parent = models.ForeignKey('self', blank=True, null=True)
    category_image = models.ImageField(upload_to='category')

    def __str__(self):
        return self.category_name

我的终端屏幕的错误截图

请帮忙。提前致谢。

旧的迁移导致了这个问题。

如果您不需要保留数据,请删除 table 并删除迁移文件夹。 再次迁移。

如果你想保留你的数据,你需要

  • 为您的新列起一个不同的名称
  • 创建一个临时列来保存转换期间的数据

然后您需要一系列迁移

架构迁移以添加新的(或临时的)列 明确移动数据的数据迁移,进行任何所需的转换(例如 "A" -> 1) 可能是模式迁移删除了您的临时列

参考:"cannot be cast to type integer" error