RuntimeWarning for DateField in Django
您好我正在使用日期搜索查询。 但我得到运行时错误。
RuntimeWarning:DateTimeField Jobs.job_created_on在时区支持处于活动状态时收到了一个天真的日期时间(2019-01-17 00:00:00)。
Views.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 - 工作模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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, ) |
为什么它给我运行时警告 - 运行时警告:DateTimeField Jobs.job_created_on在时区支持处于活动状态时收到了一个天真的日期时间(2019-01-17 00:00:00)。
当@dirkgroten突出显示时,表单会向模型中的时区感知
1 2 3 4 | 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) |