As per django docs https://docs.djangoproject.com/en/1.9/topics/db/models/
it's ORM create varchar
field instead of char
.
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
and equivalent sql statement
CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);
so here we can see it is using varchar, but can I use char
instead. I'm unable to find a way.
apart from manually altering the column
For those who struggle with the anwser given by @andrai-avram, this is what that would look like:
from django.db import models
class ActualCharField(models.CharField):
def db_type(self, connection):
varchar: str = super().db_type(connection)
char: str = varchar.replace('varchar', 'char')
return char
And then just use this class instead of the default django.db.models.CharField
I think your best bet is to write a custom field type, base it on CharField
and alter its db_type()
method. Check relevant example here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With