1
views.py问题在搜索和显示来自报告模型报告
def search(request):
"""""""
if 'search' in request.POST:
search_keyword = request.POST.get('search_keyword')
reports = reports.filter(Q(incident_description__icontains=search_keyword) | Q(incident_number__icontains=search_keyword) | Q(reportperson__name__icontains=search_keyword))
"""""""
return render(request,'search.html',{'searchform':searchform})
models.py
class Report(models.Model):
user = models.ForeignKey(User, null=False)
incident_number = models.CharField('Incident Number', max_length=100)
incident_description = models.TextField('Incident description', null=True, blank=True)
class ReportPerson(models.Model):
report = models.ForeignKey(Report)
action_type = models.CharField(max_length=100, choices=ACTION_TYPE)
name = models.CharField('Name', max_length=100)
上面视图执行关键字搜索(在模型上场数据搜索)和报告人模型。
存储在报告模型中的报告可以有两个以上的报告人详细信息。
在数据库中的值会是这样,
如果报告表和Reportperson表中的值是在图像中,报表模型是
reportperson模型
这里,报告有两个reportperson detail.If ip在这种情况下执行搜索,同样的报告会显示两次。根据报告人的详细信息数量,显示同一报告的次数显示变化。
我想知道如何处理这个,这是因为我正在使用来自Reportperson表的名称搜索。问题发生在这个report=reports.filter(....| Q(reportperson__name__icontains=search_keyword))
,。需要帮助。
如果我使用不同的搜索函数不起作用 – user2086641
使用distinct('pk')或仅使用distinct()?有一个巨大的差异... – gpichot
随着医生的帮助我解决了这个问题。谢谢加布 – user2086641