问题
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
回答1:
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
来源:https://stackoverflow.com/questions/34456801/how-to-enforce-charn-datatype-instead-of-varcharn-in-django-model-field