在Django的Model中定义TextField字段导致无法在Mysql中生成表格
2013-05-30 13:30
746 查看
转自:/article/2971958.html
class Talk(models.Model):
time = models.DateTimeField()
content = models.TextField()
定义上面的Model后,使用manage.py syncdb一直无法看到表格生成的信息,当然mysql数据库中也的确没看到生成的表格。后来发现是TextField的问题,Mysql需要特殊的设置才能使用该字段,后来在Django的官方文档上看到了下面的一段话便豁然开朗:
class TextField ([ **options ] )
A large text field. The admin represents this as a <textarea> (a multi-line
input).
MySQL users
If you are using this field with MySQLdb 1.2.1p2 and the utf8_bin collation
(which is not the default), there are some issues to be aware of. Refer to the MySQL
database notes for details.
具体可见:http://docs.djangoproject.com/en/1.1/ref/models/fields/#textfield
class TextField([**options])
A large text field. The admin represents this as a <textarea> (a multi-line input).
MySQL users
If you are using this field with MySQLdb 1.2.1p2 and the utf8_bin collation
(which is not the default), there are some issues to be aware of. Refer to the MySQL
database notes for details.
The collation setting for a column controls the order in which data is sorted as well as what strings compare as equal. It can be set on a database-wide level and also per-table and per-column. This is documented
thoroughly in the MySQL documentation. In all cases, you set the collation by directly manipulating the database tables; Django doesn’t provide a way to set this on the model definition.
By default, with a UTF-8 database, MySQL will use the utf8_general_ci_swedish collation. This results in all string equality comparisons being
done in a case-insensitive manner. That is, "Fred" and "freD" are
considered equal at the database level. If you have a unique constraint on a field, it would be illegal to try to insert both "aa" and "AA" into
the same column, since they compare as equal (and, hence, non-unique) with the default collation.
In many cases, this default will not be a problem. However, if you really want case-sensitive comparisons on a particular column or table, you would change the column or table to use the utf8_bin collation.
The main thing to be aware of in this case is that if you are using MySQLdb 1.2.2, the database backend in Django will then return bytestrings (instead of unicode strings) for any character fields it returns receive from the database. This is a strong variation
from Django’s normal practice of always returning unicode strings. It is up to you, the developer, to handle the fact that you will receive bytestrings if you configure your table(s) to use utf8_bin collation.
Django itself should work smoothly with such columns, but if your code must be prepared to call django.utils.encoding.smart_unicode() at times
if it really wants to work with consistent data – Django will not do this for you (the database backend layer and the model population layer are separated internally so the database layer doesn’t know it needs to make this conversion in this one particular
case).
If you’re using MySQLdb 1.2.1p2, Django’s standard CharField class
will return unicode strings even with utf8_bin collation. However, TextField fields
will be returned as an array.array instance (from Python’s standard array module).
There isn’t a lot Django can do about that, since, again, the information needed to make the necessary conversions isn’t available when the data is read in from the database. This problem was fixed
in MySQLdb 1.2.2, so if you want to use TextField with utf8_bincollation,
upgrading to version 1.2.2 and then dealing with the bytestrings (which shouldn’t be too difficult) is the recommended solution.
Should you decide to use utf8_bin collation for some of your tables with MySQLdb 1.2.1p2, you should still useutf8_collation_ci_swedish (the
default) collation for the django.contrib.sessions.models.Session table (usually called django_session)
and the django.contrib.admin.models.LogEntry table (usually called django_admin_log).
Those are the two standard tables that useTextField internally.
class Talk(models.Model):
time = models.DateTimeField()
content = models.TextField()
定义上面的Model后,使用manage.py syncdb一直无法看到表格生成的信息,当然mysql数据库中也的确没看到生成的表格。后来发现是TextField的问题,Mysql需要特殊的设置才能使用该字段,后来在Django的官方文档上看到了下面的一段话便豁然开朗:
TextField
class TextField ([ **options ] )A large text field. The admin represents this as a <textarea> (a multi-line
input).
MySQL users
If you are using this field with MySQLdb 1.2.1p2 and the utf8_bin collation
(which is not the default), there are some issues to be aware of. Refer to the MySQL
database notes for details.
具体可见:http://docs.djangoproject.com/en/1.1/ref/models/fields/#textfield
TextField
class TextField([**options])A large text field. The admin represents this as a <textarea> (a multi-line input).
MySQL users
If you are using this field with MySQLdb 1.2.1p2 and the utf8_bin collation
(which is not the default), there are some issues to be aware of. Refer to the MySQL
database notes for details.
Collation settings
The collation setting for a column controls the order in which data is sorted as well as what strings compare as equal. It can be set on a database-wide level and also per-table and per-column. This is documentedthoroughly in the MySQL documentation. In all cases, you set the collation by directly manipulating the database tables; Django doesn’t provide a way to set this on the model definition.
By default, with a UTF-8 database, MySQL will use the utf8_general_ci_swedish collation. This results in all string equality comparisons being
done in a case-insensitive manner. That is, "Fred" and "freD" are
considered equal at the database level. If you have a unique constraint on a field, it would be illegal to try to insert both "aa" and "AA" into
the same column, since they compare as equal (and, hence, non-unique) with the default collation.
In many cases, this default will not be a problem. However, if you really want case-sensitive comparisons on a particular column or table, you would change the column or table to use the utf8_bin collation.
The main thing to be aware of in this case is that if you are using MySQLdb 1.2.2, the database backend in Django will then return bytestrings (instead of unicode strings) for any character fields it returns receive from the database. This is a strong variation
from Django’s normal practice of always returning unicode strings. It is up to you, the developer, to handle the fact that you will receive bytestrings if you configure your table(s) to use utf8_bin collation.
Django itself should work smoothly with such columns, but if your code must be prepared to call django.utils.encoding.smart_unicode() at times
if it really wants to work with consistent data – Django will not do this for you (the database backend layer and the model population layer are separated internally so the database layer doesn’t know it needs to make this conversion in this one particular
case).
If you’re using MySQLdb 1.2.1p2, Django’s standard CharField class
will return unicode strings even with utf8_bin collation. However, TextField fields
will be returned as an array.array instance (from Python’s standard array module).
There isn’t a lot Django can do about that, since, again, the information needed to make the necessary conversions isn’t available when the data is read in from the database. This problem was fixed
in MySQLdb 1.2.2, so if you want to use TextField with utf8_bincollation,
upgrading to version 1.2.2 and then dealing with the bytestrings (which shouldn’t be too difficult) is the recommended solution.
Should you decide to use utf8_bin collation for some of your tables with MySQLdb 1.2.1p2, you should still useutf8_collation_ci_swedish (the
default) collation for the django.contrib.sessions.models.Session table (usually called django_session)
and the django.contrib.admin.models.LogEntry table (usually called django_admin_log).
Those are the two standard tables that useTextField internally.
相关文章推荐
- 在Django的Model中定义TextField字段导致无法在Mysql中生成表格
- 转载django model字段field类型
- textFieldShouldEndEditing 委托导致无法ResignFirstResponder 的一个 Bug
- Django文档——Model字段类型(Field Types)
- Django文档——Model字段类型(Field Types)
- (四)yii修改表结构导致model创建表单出现字段为定义
- mybatis-generator 无法自动生成字段类型为text的属性
- Django 模型字段类型总结 (Django Model field types summary)
- mysql 的text字段无法取值问题
- django migrate生成表结构DateTimeField 类型加了6位精度别的框架无法调用的问题?
- 没有继承Serializable导致JPA无法生成元模型(MetaModel)
- Django Model字段类型(Field Types)
- mybatis下无法通过service注入查询到mysql的text的字段信息的解决办法
- mysql中列出所有表格的对应字段
- 关于django model postgres的json字段编码问题
- Struts中实体类的字段名称要对应jsp中textfield中name属性值
- PHP FOR MYSQL 代码生成助手(根据Mysql里的字段自动生成类文件的)
- mysql存储过程变量名与字段名重复导致查不到数据
- Django生成表和反向生成Model
- [Extjs6]Ext.data.Model的id field字段必须存在