当使用ExtractYear()进行简单的算术运算时,django似乎无法创建正确的MySQL查询。
我有一个带有两个数据字段的模型:
class Event(models.Model):
start_date = models.DateField()
finish_date = models.DateField()
如果我想查找所有行,以使start_date和finish_date的年份相等,则可以这样做:
from django.db.models import F
from django.db.models.functions import ExtractYear
>>> print Event.objects.annotate(year=ExtractYear(F('start_date'))).filter(finish_date__year=F('year')).only('pk').query
SELECT `event`.`id`, EXTRACT(YEAR FROM `event`.`start_date`) AS `year` FROM `event` WHERE EXTRACT(YEAR FROM `event`.`finish_date`) = (EXTRACT(YEAR FROM `event`.`start_date`))
但是,如果我想查找在随后的几年中开始和结束的事件,请尝试使用以下过滤器,它给我一个不正确的MySQL查询:
>>> print Event.objects.annotate(year=ExtractYear(F('start_date'))).filter(finish_date__year=F('year')+1).only('pk').query
SELECT `event`.`id`, EXTRACT(YEAR FROM `event`.`start_date`) AS `year` FROM `event` WHERE `event`.`finish_date` BETWEEN 0001-01-01 AND 0001-12-31
我尝试将F('year')+1
替换为F('year')+Value(1)
,但没有帮助。
我在某个地方错了吗,还是看起来像是Django中的错误?
如果您看到其他方法可以做到这一点,我也将不胜感激!
答案 0 :(得分:1)
查询似乎合法。
但是,在我的情况下,另一个查询有效:
Event.objects.annotate(s_year=ExtractYear(F('start_date')), f_year=ExtractYear(F('finish_date')), diff=F("f_year")-F("s_year")).filter(diff=1).only('pk')
只需标注开始日期,完成日期和差异。
答案 1 :(得分:0)
这终于奏效了:
public sealed class Client
{
private string username = "";
private string _platform = "";
private readonly HubConnection _connection;
private readonly IHubProxy _proxy;
public string _Username
{
get { return username; }
set { username = value; }
}
public string _Platform
{
get { return _platform; }
set { _platform = value; }
}
public event EventHandler<string> OnMessageReceived;
public static Client instance = null;
public Client(string name, string username, string firstname, string lastname, string company, string department, string section)
{
_Username = username;
_Platform = name;
_platform = _Platform;
Dictionary<string, string> queryString = new Dictionary<string, string>();
queryString.Add("username", username);
queryString.Add("firstname", firstname);
queryString.Add("lastname", lastname);
queryString.Add("company", company);
queryString.Add("department", department);
queryString.Add("section", section);
_connection = new HubConnection("https://www.example.com/SignalRhub",queryString );
_proxy = _connection.CreateHubProxy("chathub");
}
public static Client Getinstance(string name, string username, string firstname, string lastname)
{
// create the instance only if the instance is null
if (instance == null)
{
// The username and user's name are set before instantiation
instance = new Client(name, username, firstname, lastname,"","","");
}
// Otherwise return the already existing instance
return instance;
}
public async Task Connect()
{
await _connection.Start(); //.ContinueWith._connection.server.registerMeAs("");
_proxy.On("broadcastMessage", (string platform, string message) =>
{
if (OnMessageReceived != null)
OnMessageReceived(this, string.Format("{0}: {1}", platform, message));
});
}
public async Task<List<string>> ConnectedUsers()
{
List<string> Users = await _proxy.Invoke<List<string>>("getConnectedUsers");
return Users;
}
public async Task<List<string>> ConnectedDeptUsers(string company, string dept, string section, string username)
{
List<string> Users = await _proxy.Invoke<List<string>>("getConnectedTeamUsers",company, dept, section, username);
return Users;
}
public Task Send(string message)
{
return _proxy.Invoke("Send", _platform, message);
}
public Task SendTeamMessage(string company, string dept, string section, string name, string message)
{
return _proxy.Invoke("messageTeam", company, dept, section, name, message);
}
public Task SendCompanyMessage(string company, string dept, string section, string name, string message)
{
return _proxy.Invoke("messageCompany", company, dept, message);
}
}
好像django中有一个错误。我将其添加到https://code.djangoproject.com/ticket/30494中。