问题描述
你好吗?
我有一个模型,它有两个名为 start_time
和 end_time
的字段,它们都是 CharField,并且这个模型与另一个具有名为 date
的字段的模型有关系,它是一个日期字段。看起来像这样
class A(models.Model):
date = models.DateField()
class B(models.Model):
relation = models.ForeignKey(A)
start_time = models.CharField()
end_time = models.CharField()
我需要查询模型 B,例如使用日期时间值:
B.models.filter(reconstructed_datetime="2021-04-19 15:02:00")
其中 reconstructed_datetime
是 date
(来自模型 A)与 start_time
或 end_time
(来自模型 B)连接
我尝试过这样的事情:
B.objects.annotate(
reconstructed_start=Concat(
F('relation__date'),Value(' '),F('start_date'),output_field=CharField()
)
).annotate(
reconstructed_start_datetime=Cast("reconstructed_start",DateTimeField()),)
但后来我尝试过滤
.filter(reconstructed_start_datetime="2021-04-19 15:02:00")
并且它不返回我的数据库记录
这里的“hora_inicio”是start_time
。我想使用 Cast
不起作用
那么,您知道一种连接这些模型值然后作为日期时间字段进行查询的方法吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)