I was trying to run my Django App with PostgreSQL as my database engine. I'm getting a template rendering error from the template loader after deployment. It's for the form I created using ModelChoiceField.
And also while migrating I get an error saying
django.db.utils.ProgrammingError: column "category_parent" cannot be cast automatically to type integer HINT: You might need to specify "USING category_parent::integer".
I believe the returning of index from the select field is causing the problem. Is it possible to resolve this without much trouble.
This is the class for that form.
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',)
Screenshot of the template error I'm getting.
My model class for the same
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
Screenshot of error from my terminal screen
Please help. Thanks in advance.
Old migrations caused the problem.
If you don't need to retain the data, drop the table and delete migrations folder. Migrate again.
if you want to keep your data, you need to either
you then need a sequence of migrations
a schema migration to add the new (or temp) column a data migration that explicitly moves the data, doing any required conversion (e.g. "A" -> 1) possibly a schema migration deleting your temporary column
Reference: "cannot be cast to type integer" error