django-pyodbc-azure icon indicating copy to clipboard operation
django-pyodbc-azure copied to clipboard

CharField always create nvarchar type - possible to change to varchar?

Open jonprasetyo opened this issue 8 years ago • 2 comments

The CharField create column type of nvarchar by default - is it possible to change from nvarchar to varchar?

Thanks

jonprasetyo avatar May 30 '16 07:05 jonprasetyo

my solution is define you own django field define varchar field class VarCharField(models.Field):

def __init__(self, max_length, *args, **kwargs):
    self.max_length = max_length
    super(VarCharField, self).__init__(max_length=max_length, *args, **kwargs)

def db_type(self, connection):
    """
    限定生成数据库表的字段类型为 char,长度为 max_length 指定的值
    """
    return 'varchar(%s)' % self.max_length

then use it in models.py a = VarCharField(max_length=64)

johnson329 avatar Aug 28 '19 09:08 johnson329

I know this is a very old issue and response, but does it work? I have been trying to change the main database wrapper in my project to apply specific formats to varchar and dates. However, I don't know how to do it, without modifying base.py .

@johnson329, My Django project has several apps, each with its own models.py file. Where you recomend to add this class modification to affect all apps?

EstebanHelpDesk avatar Nov 18 '23 18:11 EstebanHelpDesk