我正在学习芹菜教程。他们正在使用 Python2,而我正在尝试使用 python3 实现相同的功能。
我有2个文件:
celery_proj.py :
from celery import Celery
app = Celery(
'proj', broker='amqp://', backend='amqp://', include=['proj.tasks'])
app.conf.update(Celery_TAST_RESULT_EXPIRES=3600,)
if __name__ == '__main__':
app.start()
Run Code Online (Sandbox Code Playgroud)
和任务.py:
from celery_proj import app
@app.task
def add(x, y):
return x + y
@app.task
def mul(x, y):
return x * y
@app.task
def xsum(numbers):
return sum(numbers)
Run Code Online (Sandbox Code Playgroud)
当我尝试跑步时,celery -A proj worker -l info
我得到:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/celery/app/utils.py", line 235, in find_app
found = sym.app
AttributeError: 'module' object has no attribute …
Run Code Online (Sandbox Code Playgroud) 假设有这样的结构:
PARTICIPATION_STATUSES = (
(0, 'No, thanks'),
(1, 'I may attend'),
(2, 'I\'ll be there'),
)
class Model1(models.Model):
# ...
class Model2(models.Model):
status = models.PositiveIntegerField(
_('participation status'), choices=PARTICIPATION_STATUSES)
field = models.ForeignKey(Model1, related_name='model1_participation')
Run Code Online (Sandbox Code Playgroud)
我想要做的是Model1
用Model2
状态等于特定值的对象计数来注释每个对象(状态号是这个特定的例子).
在我的伪代码中,它看起来像:
queryset = Model1.objects.all()
queryset.annotate(declined=Count('model1_participation__status=0'))
queryset.annotate(not_sure=Count('model1_participation__status=1'))
queryset.annotate(accepted=Count('model1_participation__status=2'))
Run Code Online (Sandbox Code Playgroud)
但是我无法以这种方式注释查询集,因为Django无法解析status=<n>
.
什么是实现我想要的正确方法?
有
# example.py
def foo(arg=bar()):
pass
Run Code Online (Sandbox Code Playgroud)
bar
甚至会执行from example import foo
.
我记得很久以前我看过类似的东西:
# example.py
def foo(arg=lambda: bar()):
pass
Run Code Online (Sandbox Code Playgroud)
但我不确定这是不是最好的方式,现在,当我遇到这种情况时,我找不到任何关于如何处理这种行为的信息.
在python中将函数调用作为默认函数参数的正确方法是什么?
文档说该期间应该是以下之一:('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day')
。我很好奇我是否可以将期间设置为类似的1/10min
?
我想将我settings.AUTH_USER_MODEL
的模型添加到我的管理员中User
。我用在文档中找到的代码片段注册它:
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserChangeForm
add_form = UserCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ('email', 'bdate')
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal info', {'fields': ('bdate', 'website', 'location')}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides …
Run Code Online (Sandbox Code Playgroud) 我正在学习使用Ajax和Django,许多教程只是检查是否request.method == 'GET'
或POST.我很好奇我们.is_ajax()
当时需要什么.是否正常不使用它或教程只显示基本概念?
我有一个目录tests
,其中包含许多不同的测试名称test_*
.
我试图跑,coverage run tests
但它不起作用.
如何运行单个命令覆盖目录中的多个文件?
有一个像
['http:host1', 'http:host2', 'http:host3', 'https:host1', 'https:host4']
Run Code Online (Sandbox Code Playgroud)
我想生成一个对的列表,其中对具有相同的主机,但模式不同:
[('http:host1', 'https:host1'), ('http:host2'), ...]
Run Code Online (Sandbox Code Playgroud)
我可以很容易地分离模式标准:
with_https = [x for x in li if x.startswith('https')]
Run Code Online (Sandbox Code Playgroud)
但想不出一个优雅的解决方案来满足主机标准