我有一个看起来像这样的表:
+-------+---------+---------------------+-------------+-----------+-----------------+
| id | user_id | visit_time | action | user_type | erp_customer_id |
+-------+---------+---------------------+-------------+-----------+-----------------+
| 17460 | 818 | 2017-05-15 15:02:13 | NULL | customer | 932 |
| 17459 | 818 | 2017-05-15 15:02:11 | NULL | customer | 932 |
| 17458 | 818 | 2017-05-15 15:01:56 | NULL | customer | 932 |
| 17457 | 818 | 2017-05-15 15:01:55 | NULL | customer | 932 |
| 17456 | 818 | 2017-05-15 15:01:47 | NULL | customer | 932 |
| 17455 | 818 | 2017-05-15 15:01:15 | NULL | customer | 932 |
| 17454 | 818 | 2017-05-15 15:00:44 | NULL | customer | 932 |
| 17453 | 818 | 2017-05-15 14:59:58 | NULL | customer | 932 |
| 17452 | 818 | 2017-05-15 14:59:55 | NULL | customer | 932 |
| 17451 | 818 | 2017-05-15 14:59:55 | NULL | customer | 932 |
| 17450 | 818 | 2017-05-15 14:59:52 | NULL | customer | 932 |
| 17449 | 817 | 2017-05-15 14:55:46 | NULL | customer | 931 |
| 17448 | 817 | 2017-05-15 14:55:45 | NULL | customer | 931 |
| 17447 | 817 | 2017-05-15 14:53:55 | NULL | customer | 931 |
| 17446 | 817 | 2017-05-15 14:53:54 | NULL | customer | 931 |
| 17445 | 817 | 2017-05-15 14:53:26 | NULL | customer | 931 |
user_id是外键,visit_time是访问页面的时间戳。
想法是找出每个用户一天的访问次数。应为每个用户的每一天返回单独的一行。
所以它应该是这样的:
{'user' : 818 , 'day' : 2017-05-15 , 'visits' : 12}
{'user' : 817 , 'day' : 2017-05-15 , 'visits' : 5 }
我正在尝试使用 Django ORM 构建查询。
这是模型定义:
class Log(models.Model):
class Meta:
db_table = "users_log"
managed = False
user = models.ForeignKey(LoginCustomer , db_column = "user_id")
customer = models.ForeignKey(Customer , db_column = "erp_customer_id")
visit_time = models.DateTimeField()
user_type = models.CharField(max_length = 25)
我已经能够使用以下方法从 visit_time 中提取日期:
Log.objects.annotate(day = RawSQL('DATE(visit_time)',[]))
如何使用 Django ORM 获取所需的数据?
正在使用的数据库是 MySQL。
最佳答案
你需要使用
from django.db.models import Count
from django.db.models.functions import TruncDay
Log.objects
.annotate(period=TruncDay('visit_time'))
.values('period', 'user_id')
.annotate(visits=Count('id'))
.values('period', 'visits')
关于python - Django 从特定用户的日期时间字段计算访问日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43977854/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
这里是Ruby新手。完成一些练习后碰壁了。练习:计算一系列成绩的字母等级创建一个方法get_grade来接受测试分数数组。数组中的每个分数应介于0和100之间,其中100是最大分数。计算平均分并将字母等级作为字符串返回,即“A”、“B”、“C”、“D”、“E”或“F”。我一直返回错误:avg.rb:1:syntaxerror,unexpectedtLBRACK,expecting')'defget_grade([100,90,80])^avg.rb:1:syntaxerror,unexpected')',expecting$end这是我目前所拥有的。我想坚持使用下面的方法或.join,
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
我的日期格式如下:"%d-%m-%Y"(例如,今天的日期为07-09-2015),我想看看是不是在过去的七天内。谁能推荐一种方法? 最佳答案 你可以这样做:require"date"Date.today-7 关于ruby-检查日期是否在过去7天内,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/32438063/
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build