问题描述
您好,我正在使用日期进行搜索查询。但是我遇到了运行时错误。
Hello i'm using dates for search queries. But i am getting runtime error.
RuntimeWarning:DateTimeField Jobs.job_created_on在激活时区支持时收到了一个简单的日期时间(2019-01-17 00:00:00)
RuntimeWarning: DateTimeField Jobs.job_created_on received a naive datetime (2019-01-17 00:00:00) while time zone support is active.
Views.py
class JobListView(LoginRequiredMixin, generic.TemplateView):
template_name = 'admin/jobs/job.html'
def get(self, request, *args, **kwargs):
context = super(JobListView, self).get_context_data(**kwargs)
if 'status' in request.GET:
form = JobSearchForm(request.GET)
if form.is_valid():
status = form.cleaned_data['status']
start_date = form.cleaned_data['start_date']
end_date = form.cleaned_data['end_date']
company = self.request.user.userprofile.user_company
lookups = (Q(job_company=self.request.user.userprofile.user_company) )
if start_date:
lookups = lookups | Q(job_created_on__gte=start_date)
if end_date:
lookups = lookups | Q(job_created_on__lte=end_date)
jobs=Jobs.objects.exclude(job_is_deleted = True).filter(lookups)
else:
form = JobSearchForm()
company_name = self.request.user.userprofile.user_company
jobs = Jobs.objects.exclude(
job_is_deleted = True
).filter(
job_company=self.request.user.userprofile.user_company
)
return render(request, self.template_name, {'form': form, 'jobs': jobs})
Forms.py
ACTIVE_CHOICES = (
('AllStatus', 'Select Status'),
('Active', 'Active'),
('InActive', 'Inactive'),
)
class JobSearchForm(forms.Form):
start_date = forms.DateField(label=False)
end_date = forms.DateField(label=False)
status = forms.ChoiceField(choices=ACTIVE_CHOICES, label=False, initial="")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['start_date'].widget.attrs['placeholder'] = 'Start Date'
self.fields['end_date'].widget.attrs['placeholder'] = 'End Date'
# self.fields['status'].initial = 'Select Status'
self.fields['start_date'].widget.attrs['class'] = 'job_date'
self.fields['end_date'].widget.attrs['class'] = 'job_date'
self.fields['start_date'].required=False
self.fields['end_date'].required=False
Models.py-工作模型
Models.py - Jobs Models
class Jobs(models.Model):
job_created_on = models.DateTimeField(auto_now_add=True)
job_created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='job_created_bys')
job_updated_on = models.DateTimeField(auto_now=True)
job_updated_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='job_updated_bys')
job_is_deleted = models.BooleanField(default=False)
job_deleted_at = models.DateTimeField(blank=True, null=True)
ACTIVE = 'Active'
INACTIVE = 'Inactive'
JOB_ACTIVE_CHOICES = (
(ACTIVE, 'Active'),
(INACTIVE, 'Inactive'),
)
job_status = models.CharField(
max_length=8,
choices=JOB_ACTIVE_CHOICES,
default=INACTIVE,
)
为什么给我运行时警告-RuntimeWarning:DateTimeField Jobs.job_created_on收到的日期时间很短(2019-01-17 00: 00:00)时区支持处于活动状态。
Why it is giving me runtime warning - RuntimeWarning: DateTimeField Jobs.job_created_on received a naive datetime (2019-01-17 00:00:00) while time zone support is active.
推荐答案
突出显示@dirkgroten,您的表单将返回 Date
(没有时区)到模型中的时区感知 DateTime
。这是使用查找:
As @dirkgroten highlighted, your form returns a Date
, which doesn't have a timezone, to a timezone-aware DateTime
in your model. Here is how fix that with the date
lookup:
if start_date:
lookups = lookups | Q(job_created_on__date__gte=start_date)
if end_date:
lookups = lookups | Q(job_created_on__date__lte=end_date)
这篇关于Django中DateField的RuntimeWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!