我有一个表格,可以按年份和月份对学生进行筛选。如果按年份过滤,此代码可以正常工作,但如果按月份过滤,则此代码将不工作。我该如何解决? 它没有显示该月的可用数据。
models.py
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<minimizeJar>true</minimizeJar>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>fully.qualified.path.to.Main</Main-Class>
<Built-On>${maven.build.timestamp} UTC</Built-On>
<ModuleName>${project.name}</ModuleName>
<ModuleVersion>${project.version}</ModuleVersion>
<BaseVersion>${baseversion}</BaseVersion>
<BuildNumber>${buildnumber}</BuildNumber>
<GITRevision>${gitrevision}</GITRevision>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</plugin>
views.py
class Student(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
phone = models.CharField(max_length=15)
joined_date = models.DateField()
模板
def serachstudent(request):
q = request.GET["q"]
if q:
students = Student.objects.filter(Q(joined_date__year__icontains=q)|Q(joined_date__month__icontains=q))
return render(request, "students/view_students.html", {'students': students})
else:
return redirect('students:view_student')
答案 0 :(得分:1)
只需执行
students = Student.objects.filter(Q(joined_date__year=q)|Q(joined_date__month=q))