django model改变后,同步数据库
在使用django进行开发时,往往需要根据不同的需求对model进行更改。而这时候,python manage.py syncdb就不好使了。
目前有个很好的工具,是south,这个是专门用来更改model的。官方的文档见http://south.readthedocs.org/en/latest/index.html。 里面有详述的文档,包括安装和使用。
安装很简单easy_install 可以。
然后在你的setting.py 里面的app_install 里添加 'south'。
然后就可以使用命令 python manage.py schemamigration app --initial 这个是在你更改model之前做的初始化,有了这个版本后,你就可以随便对app里的model进行更改了。更改结束后,运行python manage.py schemamigration app --auto然后使用python manage.py migrate app 就可以了。
problems:
1。如果添加一个字段,south需要你给这个字段添加一个默认直,以供之前已经存在的记录作为默认值。但是,如果这个字段是unique的话,怎么设置默认值就都不行了。这个时候可能需要你直接对数据库的表进行操作,删除所有已经存在的记录,然后设置默认值。另一个方法是,先不设定unique,添加字段,然后通过网站的后台更改已经存在的记录的这个字段为不同值,然后再设定其为unique。后者是我的想法,没有实践过,提供一种思路。
2。有时可能需要对某些字段添加自己的解析规则。因为有些custom字段是Cannot freeze field 的,需要自己写规则,
比如该类:在core.fields.thumbs.py 中的类 ImageWithThumbsField
from django.db.models import ImageField
class ImageWithThumbsField(ImageField):
def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, sizes=None, **kwargs): self.verbose_name=verbose_name self.name=name self.width_field=width_field self.height_field=height_field self.sizes = sizes super(ImageField, self).__init__(**kwargs)
而我在core.models.py中用到了这个类,我可以在core.models.py的开头加入这个:
from south.modelsinspector import add_introspection_rules from core.fields.thumbs import ImageWithThumbsField add_introspection_rules( [ ( (ImageWithThumbsField, ), [], { "verbose_name": ["verbose_name", {"default": None}], "name": ["name", {"default": None}], "width_field": ["width_field", {"default": None}], "height_field": ["height_field", {"default": None}], "sizes": ["sizes", {"default": None}], }, ), ], ["^core.fields.ImageWithThumbsField",])
即可。更多的关于怎么自己写rule,可以参考:
http://south.readthedocs.org/en/latest/customfields.html#extending-introspection
http://stackoverflow.com/questions/4715964/django-south-introspection-rule-doesnt-work
3如果在使用
./manage.py migrate myapp 的时候,如果出现某些表已经存在的错误(
django-south-table-already-exists
),可以这样:
./manage.py migrate myapp --fake 参数
参数可以是上面schemamigration 命令返回的版本。
参考:
http://stackoverflow.com/questions/3090648/django-south-table-already-exists
http://stackoverflow.com/questions/10769644/django-south-adding-new-field-but-databaseerror-occurs-table-already-exists
目前我是遇到了这些错误,为大家总结一下。
能够对model进行更改了,我们就可以进行的开源的项目中遨游啦。哈哈。
django model改变后,同步数据库的更多相关文章
- Hibernate由model类自动同步数据库表结构
在开发中遇到了个问题,每次测试数据库增加表结构的时候,本地pull下最新代码导致启动报错,上网搜了快速解决办法---->hibernate 配置属性中,hibernate.hbm2ddl.aut ...
- django model设计与实际数据库表的对比
# 文章class Article(models.Model): title = models.CharField('标题', max_length=70) excerpt = models.Text ...
- django 修改字段后,同步数据库,失败:django.db.utils.InternalError: (1054, "Unknown column 'api_config.project_id_id' in 'field list'")
问题原因是,修改字段后,同步失败了,然后执行查询的时候,就会提示这个错误,这个字段没有 最暴力的方法可以直接在数据库中修改字段,但是修改后,models没同步,可能会存在问题,因此开始我的百度之旅(这 ...
- django(python manage.py imgrate)同步数据库出错后的解决办法
问题 很多情况下,因为app的models.py的文件内容有误,但是通过python manage.py check检查不出来时,当执行python manage.py migra ...
- django(python manage.py migrate)同步数据库出错后的解决办法
执行python manage.py migrate同步数据库前一次执行 python manage.py makemigrations 时生成的文件及之后所有的文件删除即可,然后修改models.p ...
- python_way day18 html-day4, Django路由,(正则匹配页码,包含自开发分页功能), 模板, Model(jDango-ORM) : SQLite,数据库时间字段插入的方法
python_way day18 html-day4 1.Django-路由系统 - 自开发分页功能 2.模板语言:之母板的使用 3.SQLite:model(jDango-ORM) 数据库时间字 ...
- django Model模型二及Model模型对数据库的操作
在django模型中负责与数据库交互的为Model层,Model层提供了一个基于orm的交互框架 一:创建一个最基本的Model from __future__ import unicode_lite ...
- 自动化监控系统(二)连接数据库,创建app,添加model,同步数据库
数据库我使用:mysql5.7 程序连接数据库的模块:pymysql 一.创建数据库: dbname:automatedmonitor 二.使用pip安装pymysql,这里我直接在pycharm上安 ...
- 【Django】Django model与数据库操作对应关系(转)
Django对数据库的操作分用到三个类:Manager.QuerySet.Model. Manager的主要功能定义表级方法(表级方法就是影响一条或多条记录的方法),我们可以以models.Manag ...
随机推荐
- mysql查询大于X分钟数
select * from table where date_add(STR_TO_DATE(createtime,'%Y-%m-%d %T:%i:%s'), interval '00:60:00 ...
- 发现ramnit样本一枚
https://files.cnblogs.com/files/yfish/DesktopLayer.rar
- 初识STM32标准库
1.CMSIS 标准及库层次关系 CMSIS 标准中最主要的为 CMSIS 核心层,它包括了: STM32标准库可以从官网获得: 在使用库开发时,我们需要把 libraries 目录下的库函数文件添加 ...
- Eclipse配置PyDev插件(配置Python环境) 及javascript相关配置
Eclipse开发Javascript环境配置(含EXTJs配置) 来自:sayo http://www.cnblogs.com/sayo/archive////.html Eclipse开发JQue ...
- WebApi参数传递总结(转)
出处:http://www.cnblogs.com/Juvy/p/3903974.html 在WebAPI中,请求主体(HttpContent)只能被读取一次,不被缓存,只能向前读取的流. 举例子说明 ...
- scala高阶函数类型推断什么时候失效?
class TypeInfer(self: Int, other: Int) { def test(num: Int, word: String, fun1: (Int, Int) => Int ...
- 如何从官网开始 mongo java
http://docs.mongodb.org/ecosystem/drivers/ MongoDB Driver Documentation Getting Started Installation ...
- ZOJ1648 Circuit Board 2017-04-18 20:31 34人阅读 评论(0) 收藏
Circuit Board Time Limit: 2 Seconds Memory Limit: 65536 KB On the circuit board, there are lots ...
- Codeforces761B Dasha and friends 2017-02-05 23:34 162人阅读 评论(0) 收藏
B. Dasha and friends time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Java动态代理(二)CGLIB动态代理应用
JDK自从1.3版本开始,就引入了动态代理,JDK的动态代理用起来非常简单,但是它有一个限制,就是使用动态代理的对象必须实现一个或多个接口 .如果想代理没有实现接口的类可以使用CGLIB包. CGLI ...