我正在使用Postgres SERIAL作为我的主键.插入一行后,我可以使用' RETURNING'或'获取生成的密钥CURRVAL().
现在我的问题是我想在事务中进行批量插入并获取所有生成的密钥.
所有我得到的RETURNING并且CURRVAL是最后生成的id,结果的其余部分被丢弃.
我怎样才能让它归还所有这些?
谢谢
我有一个包装周围EmailMultiAlternatives使界面有点清洁(从几乎逐字这里):
class Email(object):
'''
Wrapper around the Django core's EmailMultiAlternatives that makes it simpler
to render txt and html templates.
'''
def __init__(self, to, subject):
self.to = to
self.subject = subject
self.html = None
self.text = None
def _render(self, template, context):
return render_to_string(template, context)
def render_html(self, template, context):
self.html = self._render(template, context)
def render_text(self, template, context):
self.text = self._render(template, context)
def send(self, from_addr=None, fail_silently=False):
if isinstance(self.to, basestring):
self.to = [self.to]
if not from_addr:
from_addr = settings.EMAIL_HOST_USER …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
import pyshark
capture = pyshark.LiveCapture(interface='en0')
capture.sniff()
Run Code Online (Sandbox Code Playgroud)
现在这里是错误:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
capture.sniff()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pyshark/capture/capture.py", line 109, in load_packets
self.apply_on_packets(keep_packet, timeout=timeout)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pyshark/capture/capture.py", line 201, in apply_on_packets
return self.eventloop.run_until_complete(coro)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/trollius/base_events.py", line 350, in run_until_complete
return future.result()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/trollius/futures.py", line 286, in result
raise self._exception
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/trollius/tasks.py", line 250, in _step
result = coro.throw(exc)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pyshark/capture/capture.py", line 212, in packets_from_tshark
tshark_process = yield From(self._get_tshark_process(packet_count=packet_count))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/trollius/tasks.py", line 252, in _step
result = coro.send(value) …Run Code Online (Sandbox Code Playgroud) 在我的工作中,我经常在我的django模型中有两个表,并且必须连接它们以将此数据作为csv返回.该数据未通过外键连接,但它们具有连接它们的标识符.这是因为我们从两个不同的源导入这些数据,有时缺少对应的数据,因此在创建条目时无法连接它.
我的问题是:如果你想到我经常要返回这些数据,那么在性能方面连接这些数据的最佳方法是什么?
class OrderInvoiceConnector(models.Model):
order_data = models.ForeignKey(Order, related_name="invoice")
invoice_data = models.ForeignKey(Invoice, related_name="order")
Run Code Online (Sandbox Code Playgroud)
class ConnectedData(models.Model):
invoice_id = models.CharField(max_length=255)
country_iso = models.CharField(max_length=255)
invoice_date = models.CharField(max_length=255)
tax = models.FloatField(max_length=255)
price = models.FloatField()
Run Code Online (Sandbox Code Playgroud) python django performance django-models database-performance
1. instance.save()
2. instance.foo = foo; instance.save(update_fields=['foo'])
3. InstanceClass.objects.filter(id=instance.id).update(foo=foo)
Run Code Online (Sandbox Code Playgroud)
我发现#3有时候不可靠.
在我运行#3之后,偶尔foo没有正确设置(没有提交?)
当我不想运行信号处理程序时,我常常使用#3.
什么时候使用以上3?
我正在开发一个Django项目,该项目利用Clusterize.js动态加载div中的"datapoints"列表.在Javascript/HTML方面,最初加载"blackbox"的前200个数据点.如果还有更多,则工作者一次开始加载1000个数据点,直到在滚动列表中获取并呈现所有数据点.当用户单击数据点时,将显示该数据点的详细信息.
最初,有一个错误会导致199以后的任何数据点出现错误的细节.一旦加载任何黑盒子的任何新数据点超过199,错误的数据点ID将传递给Javascript函数showDatapointDetail,datapoint 200将获得详细信息对于数据点0,数据点201会得到详细为数据点1,等等,直到数据点1199将获取的数据点999的细节.然后数据点1200将获得0和模式将重复.我已经修复了Javascript代码中的这个错误,但现在我正在编写一个很好的测试,tests.py以确保点击200以上的数据点获得正确的详细信息.该项目一直在使用Python selenium进行大量的浏览器测试,现在我试图弄清楚如何使用它滚动到动态生成的div的底部.如果我这样做:
dp_detail_199 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "datapoint199")))
Run Code Online (Sandbox Code Playgroud)
然后我可以正确地获得该数据点细节,因为最初只加载了200个.但是,如果黑盒子有787个数据点,那么以下内容将不起作用:
dp_detail_750 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "datapoint750")))
Run Code Online (Sandbox Code Playgroud)
因为需要向下滚动才能开始加载下一个数据点.那么如何使用Selenium滚动到Clusterize生成的列表的底部?div被命名dp_pane,我可以用Selenium找到它,但我不确定如何正确向下滚动,特别是向下滚动.我尝试过的一件事是首先向下滚动dp_pane:
driver.execute_script("arguments[0].scrollTop = 12000", dp_pane)
Run Code Online (Sandbox Code Playgroud)
下到datapoint 200,然后向下滚动一点,找到数据点205:
while True:
dp_pane.send_keys(Keys.PAGE_DOWN)
try:
dp_detail_205 = driver.find_element_by_id("datapoint205")
except NoSuchElementException:
######## DEBUG OUTPUT #########
print "Couldn't find dp_detail_205"
continue
else:
######## DEBUG OUTPUT #########
print "Found it!"
break
Run Code Online (Sandbox Code Playgroud)
但这似乎陷入困境,从未找到数据点205; 相反,我只是继续获得调试输出"Couldn't find dp_detail_205".我应该怎么做才能让Selenium滚动到底部,dp_pane以便我可以找到,例如,datapoint750并验证它是否具有正确的详细信息?最终,我需要的是Selenium让Clusterize加载更多的行,因为这是我需要的一件事,这个测试工作根本没用.甚至Clusterize自己的编程滚动方法也不起作用; 我将使用的行将是这样的:
document.getElementById('dp_pane').scrollTop = 12000;
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,无论我使用scrollTop值多大,它都只能达到数据点199的最低值,并且不会自动加载更多的行.
编辑:
我最近将项目升级到Django 1.9,其中包括与QUnit的集成.乍一看,我认为这是测试Selenium的理想替代品.但是,现在我在阅读一些文件后不确定; 首先,测试涉及首先上传.dat文件,我不确定QUnit可以用于.此外,它可以在Travis CI中轻松实现自动化吗?试图为此进行工作测试已经证明是艰巨的,我不确定在这一点上我还能尝试什么.
我不能让我的芹菜工人经常听默认队列.芹菜不断退出.
$: docker-compose up
Starting tasker_rabbitmq_1
Starting tasker_celery_1
Attaching to tasker_rabbitmq_1, tasker_celery_1
tasker_celery_1 exited with code 1
rabbitmq_1 |
rabbitmq_1 | RabbitMQ 3.6.1. Copyright (C) 2007-2016 Pivotal Software, Inc.
rabbitmq_1 | ## ## Licensed under the MPL. See http://www.rabbitmq.com/
rabbitmq_1 | ## ##
rabbitmq_1 | ########## Logs: /var/log/rabbitmq/rabbit@0bcd2c4762eb.log
rabbitmq_1 | ###### ## /var/log/rabbitmq/rabbit@0bcd2c4762eb-sasl.log
rabbitmq_1 | ##########
rabbitmq_1 | Starting broker... completed with 6 plugins.
Run Code Online (Sandbox Code Playgroud)
我正在尝试构建一个具有单独作业层的应用程序作为单独部署的作业容器.所以架构是:
这就是我所拥有的:
文件夹结构:
-tasker
-tasker …Run Code Online (Sandbox Code Playgroud) 在Django中,当CSRF_COOKIE_HTTPONLY设置设置为True时,CSRF cookie获得了httponly标志,这从安全角度来看是理想的,但是打破了将此cookie添加到httpProvider的标准角度解决方案,如下所示:
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
Run Code Online (Sandbox Code Playgroud)
通过Django 1.9,有一个解决方法,你可以通过将它放在模板中直接将cookie传递给应用程序:
<script>
window.csrf_token = "{{ csrf_token }}";
</script>
Run Code Online (Sandbox Code Playgroud)
把它放在角度应用程序中:
angularApp.config(["$httpProvider", function($httpProvider)e {
$httpProvider.defaults.headers.common["X-CSRFToken"] = window.csrf_token;
}]
Run Code Online (Sandbox Code Playgroud)
不幸的是,这对于Django 1.10+中的单页角度应用程序不起作用,因为CSRF cookie在每次请求后都会更改.如何通过CSRF_COOKIE_HTTPONLY设置从Angular发送到Django 1.10+的帖子请求?
注意:禁用CSRF保护是不可接受的答案.
我们django-simple-history为我们的模型做好了准备.最近,一大堆模型被神秘地删除了.事实发生后几天就注意到了这一点,因此避免完整的数据库备份恢复会很好,因为这样可以消除事后发生的手动更改.
我找不到任何方法来轻松恢复模型实例,特别是删除的模型实例.我可以查询模型的历史版本并查找已删除的所有内容.有了这个,我还可以观察到他们所有人都有删除作为他们的最后一次改变.我可以在history-1上使用instance属性来获取删除前的状态,但是如果我尝试保存它是错误的,因为模型实例已被删除而且不再存在.
基本上,如果我有django-simple-history的历史记录,那么恢复已删除模型实例的最简洁方法是什么?如果可能的话,我想保留历史记录,所以在完全重新创建对象之前,我正在研究任何解决方案.
我在这里写一个功能测试来检查我的API限制是否按预期工作(将在每个月的开始时休息).
测试类:
class ApiThrottlingTest(ThrottlingBaseTest):
def test_throttling_purchaser_case(self):
now = datetime.datetime(year=2015, month=1, day=10, hour=6, minute=6, second=3)
last_day_of_current_month = datetime.datetime(year=2015, month=1, day=31, hour=23, minute=59, second=59)
first_day_of_next_month = datetime.datetime(year=2015, month=2, day=1, hour=0, minute=0, second=0)
with freeze_time(now) as frozen_datetime:
for i in xrange(3):
resp = self._project_details_request()
self.assertEqual(resp.status_code, 200)
resp = self._project_details_request()
self.assertEqual(resp.status_code, 429)
frozen_datetime.move_to(last_day_of_current_month)
resp = self._project_details_request()
# the test fails at this level
self.assertEqual(resp.status_code, 429)
frozen_datetime.move_to(first_day_of_next_month)
resp = self._project_details_request()
self.assertEqual(resp.status_code, 200)
Run Code Online (Sandbox Code Playgroud)
如果出现以下情况,测试工作正常:last_day_of_current_month = datetime.datetime(... second=0)
但如果出现以last_day_of_current_month = datetime.datetime(... second=59)
在调试之后,似乎timeDjangoRestFramework …
django ×7
python ×6
celery ×2
angularjs ×1
cookies ×1
csrf ×1
django-csrf ×1
docker ×1
freezegun ×1
import ×1
javascript ×1
performance ×1
postgresql ×1
pyshark ×1
python-3.x ×1
selenium ×1
unit-testing ×1