我遇到了一个奇怪的问题.我在Mac OS X Yosemite上使用Django 1.7.1,我已经配置了一个本地MySQL数据库.
通常,我创建一个模型,如果我想添加另一个字段,我只需要做一个./manage.py migrate,Django创建一个新的数据库列.
我做到了那一点.这是我的模特:
from django.db import models
from django.utils.translation import ugettext as _
class Product(models.Model):
CATEGORY_CHOICES = (
('apphosting', _('Application Hosting')),
('webhosting', _('Web Hosting'))
)
category = models.CharField(max_length=25, choices=CATEGORY_CHOICES)
name = models.CharField(max_length=25)
price_monthly = models.FloatField()
def __unicode__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
请注意,我已添加该字段price_monthly.然后,我做了一个./manage.py migrate:
(mysphere)dmanser@ragamuffin:~/git/mysphere on master [!?]$ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: crispy_forms
Apply all migrations: customers, sessions, admin, sites, flatpages, contenttypes, products, auth
Synchronizing apps without migrations: …Run Code Online (Sandbox Code Playgroud) 我有一个Django 1.9应用程序,该应用程序正在运行一段代码,根据对某些远程API的查询结果,对数据库进行更改。例如,这可能是有关提交,文件更改,审阅者,拉取请求等的数据,这些数据我想另存为数据库中的实体。
commit_data = commit_API_client.get_commit_info(argument1, argument2)
new_commit = models.Commit.Create(**commit_data)
#if the last API failed, this will fail
#I will need to run this again to get these files, so I need
#to get the commit all over again, too
files = file_API_client.get_file_info(new_commit.id)
new_files = models.Files.Create(**files)
#do some more stuff here
Run Code Online (Sandbox Code Playgroud)
我正在调用的少数几个API之一很有可能会返回一些错误而不是有效数据。实际上,我基本上需要使这一部分成为一个原子事务,以便如果从中没有返回错误HTTP requests,我会将所有更改提交给数据库。否则,如果2个API正确返回,我可能会丢失一些数据,但第3个不会。
我看到Django支持commit hooks数据库事务,但是我想知道这是否适用于这种情况,以及我将如何实现这一点。
我有一张如下表
select id, name from node;
+----+------+
| id | name |
+----+------+
| 5 | na |
+----+------+
Run Code Online (Sandbox Code Playgroud)
然后定义下面的函数
>>> def foo_with_sfu(seconds):
... with transaction.atomic():
... node = Node.objects.select_for_update().filter(pk=5)
... time.sleep(seconds)
... node = Node.objects.get(pk=5)
... print(node.name)
...
Run Code Online (Sandbox Code Playgroud)
我希望 select_for_update 会锁定行 pk=5 所以如果我打开另一个控制台在 time.sleep 期间更改 node.name,更改操作将被阻止。
但实际上当我运行该函数,并在 time.sleep 期间在另一个控制台中运行 update sql 时,更新没有被阻止。
似乎选择更新没有锁定行。为什么?
\u2264 这是字符(小于或等于),这是错误的根本原因。详细错误日志:
Traceback (most recent call last):
File "C:\Dev\EXE\TEMP\cookie\crumbs\views.py", line 1520, in parser
html_file.write(html_text)
File "C:\Users\Cookie1\AppData\Local\Programs\Python\Python36-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2264' in position 389078: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)