How to enforce char(N) datatype instead of varchar(N) in django model field

牧云@^-^@ 提交于 2019-12-01 06:13:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!