我得到了简单的要求(不是简单的实现),并且在没有对db进行多次命中的情况下确定如何实现它,并且没有.extra()在queryset中.
Task:
name = xxx
status = models.IntegerField(choices=some_choices)
project = ForeignKey(Project)
Project:
name = xxx
code = xxx
Run Code Online (Sandbox Code Playgroud)
项目包含具有各种状态的任务.(假设status = 3已完成)现在,我想列出所有项目的总任务和已完成的任务,如下所示
我能够使用注释获得total_tasks,但不能获得completed_tasks,因为它需要注释中的条件.无论如何要做到这一点?
我正在尝试使用Django的注释功能将相关模型的计数添加到查询集.但是,我不想完全计算相关对象,我只想计算活动对象(即"is_active = True").我无法弄清楚如何过滤掉计数.
(简化)相关模型:
class Post(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=80)
body = models.TextField()
class Comment(models.Model):
user = models.ForeignKey(User)
post = models.ForeignKey(Post)
comment_body = models.CharField(max_length=80)
is_active = models.BooleanField(default=True)
Run Code Online (Sandbox Code Playgroud)
在视图中,我试图注释一个查询集:
queryset=Post.objects.all().annotate(num_comments=Count('comment', distinct=True))
Run Code Online (Sandbox Code Playgroud)
以上计算与帖子相关的所有评论,而我只想计算"is_active"评论.Google和Django文档在这里没有帮助我.有没有人解决过这个问题?
我有一个看起来像这样的记录集(省略了无关的数据以保护容易无聊):
id | user_id | created | units
----+---------+-------------------------------+-------
1 | 1 | 2011-04-18 15:43:02.737063+00 | 20
2 | 1 | 2011-04-18 15:43:02.737063+00 | 4
3 | 1 | 2011-04-18 15:46:48.592999+00 | -1
4 | 1 | 2011-04-19 12:02:10.687587+00 | -1
5 | 1 | 2011-04-19 12:09:20.039543+00 | -1
6 | 1 | 2011-04-19 12:11:21.948494+00 | -1
7 | 1 | 2011-04-19 12:15:51.544394+00 | -1
8 | 1 | 2011-04-19 12:16:44.623655+00 | -1
Run Code Online (Sandbox Code Playgroud)
我希望得到一个看起来像这样的结果:
id | user_id | created | …Run Code Online (Sandbox Code Playgroud) 我们在Visual Studio 2010中使用Hg.我希望能够看到谁编辑了编辑器中的代码而不必启动VisualHg或命令提示符.
有没有人知道一个插件,在编辑器的边缘显示用户?
即使我使用VisualHG的注释功能只显示更改集,然后您必须查找谁提交了更改集.
无法在ggplot注释中添加希腊字母,当其中之一:它夹在其他文本之间,或者所讨论的文本包含撇号.
例如,以下工作正常:
df <- data.frame(x = rnorm(10), y = rnorm(10))
temp<-paste("rho == 0.34")
ggplot(df, aes(x = x, y = y)) + geom_point() +
annotate("text", x = mean(df$x), y = mean(df$y), parse=T,label = temp)
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,ggplot会爆炸:
df <- data.frame(x = rnorm(10), y = rnorm(10))
temp<-paste("Spearman's rho == 0.34")
ggplot(df, aes(x = x, y = y)) + geom_point() +
annotate("text", x = mean(df$x), y = mean(df$y), parse=T,label = temp)
Run Code Online (Sandbox Code Playgroud)
ggplot对这些特殊字符非常敏感.其他帖子似乎没有解决这个问题(如果没有,请道歉).提前致谢.
我试图用一个标签标注ggplot2图,该图表明X1〜N(mu = 10,sigma = 3)的分布,其中1表示下标,如下所示:
label1<-"X[1] ~ N( \U03bc = 10, \U03c3 = 3)"
Run Code Online (Sandbox Code Playgroud)
当我像这样使用label1时:
library(ggplot2)
label1<-"X[1] ~ N( \U03bc = 10, \U03c3 = 3)"
ggplot() + annotate("text", x=18,y=0.05,label=label1)
Run Code Online (Sandbox Code Playgroud)
下标将被忽略,剩下的就是所需的。
当我像这样使用label1时:
library(ggplot2)
label1<-"X[1] ~ N( \U03bc = 10, \U03c3 = 3)"
ggplot() + annotate("text", x=18,y=0.05,label=label1,parse=TRUE)
Run Code Online (Sandbox Code Playgroud)
下标绘制正确,但是波浪符号在一个空格中转换。
有什么建议如何使下标和波浪号发生吗?谢谢!
我试图用一些阴影将一些文本注释到基本图像上.我不喜欢使用-shadow选项获得的结果,所以我放下文本,模糊它,然后再次将文本放在白色,偏离阴影的几个像素.这是我正在使用的命令:
convert base_image.jpg \
-font TT0590M_.ttf \
-fill gray30 \
-annotate +0+0 '' -gravity North \
-annotate +72+32 'ABCDEFGHIJKLM' \
-blur 0x4 \
-fill white \
-annotate +72+27 'ABCDEFGHIJKLM' \
combined.png
Run Code Online (Sandbox Code Playgroud)
我的问题是-blur选项不仅模糊了第一层文本,还模糊了底层基本图像.我只希望第一层文字模糊,而不是基本图像.
我读了一些关于使用堆栈的内容,并尝试使用\( \)该部分隔离第一层文本和模糊命令,如下所示:
convert base_image.jpg \
-font TT0590M_.ttf \
-fill gray30 \
-annotate +0+0 '' -gravity North \
\( -annotate +72+32 'ABCDEFGHIJKLM' \
-blur 0x4 \) \
-fill white \
-annotate +72+27 'ABCDEFGHIJKLM' \
combined.png
Run Code Online (Sandbox Code Playgroud)
结果是相同的 - 文本阴影和底层基础图像都变得模糊.我恐怕我没有太多的运气理解堆栈或我应该使用什么其他命令来获得我所追求的效果.
我正在使用 pypy 将一些 python 脚本翻译成 C 语言。假设我有一个这样的 python 类:
class A:
def __init__(self):
self.a = 0
def func(self):
pass
Run Code Online (Sandbox Code Playgroud)
我注意到这A.func是一个未绑定的方法而不是一个函数,因此它不能被 pypy 翻译。所以我稍微修改一下代码:
def func(self):
pass
class A:
def __init__(self):
self.a = 0
A.func = func
def target(*args):
return func, None
Run Code Online (Sandbox Code Playgroud)
现在func好像可以用pypy翻译了。但是,当我尝试时translate.py --source test.py,[translation:ERROR] TypeError: signature mismatch: func() takes exactly 2 arguments (1 given)会引发异常。我注意到这可能是因为我还没有注释self参数。但是这self有类型 A,那么我如何注释一个类呢?
感谢您的阅读和回答。
我试图在我通过函数创建的条形图上注释标准消息。以下是代码:
hashbar <- function(x) {
suppressWarnings(library(stringr))
hash <- "#[A-Za-z0-9]{1,}"
hashtg <- str_extract_all(x$text, hash)
hashtg <- as.data.frame(unlist(hashtg))
hashtg <- as.data.frame(sort(table(hashtg), decreasing = TRUE)[1:15])
names(hashtg)[1] <- "Freq"
hashtg$hashtag <- rownames(hashtg)
rownames(hashtg) <- NULL
suppressWarnings(library(ggplot2))
suppressWarnings(library(RColorBrewer))
p <- ggplot(hashtg, aes(x=reorder(hashtag, Freq), y = Freq, fill = hashtag)) + geom_bar(stat="identity") +
geom_bar(width = 0.4) + xlab("Hashtags Used") + ylab("Number of responses") +
geom_text(aes(label=Freq), hjust = 1, colour = "white" ) +
ggtitle("Analysis of Most Frequently Used Hashtags") +
theme(plot.title=element_text(size=rel(1.2), lineheight = 1, face = …Run Code Online (Sandbox Code Playgroud) 我有一个模型:
class Motocycle(models.Model):
title = models.CharField(max_length=50, blank=True, default='')
engine_displacement = models.IntegerField(default=0)
Run Code Online (Sandbox Code Playgroud)
我想:
queryset = Motocycle.objects.annotate(
full_name=Concat(
'title',
Value(' '),
F('engine_displacement'),
Value('')
),
).all()
Run Code Online (Sandbox Code Playgroud)
但是出现错误Expression contains mixed types. You must set output_field::
queryset = Motocycle.objects.annotate(
full_name=Concat(
'title',
Value(' '),
F('engine_displacement'),
Value(''),
),
output_field=CharField(),
).all()
Run Code Online (Sandbox Code Playgroud)
我试图设置这个output_field,结果是:'CharField' object has no attribute 'resolve_expression'。
我做错了什么?谢谢你。