1

ビュー.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)

上記のビューはReportモデルとReportpersonモデルからキーワード検索(モデルフィールドのデータの検索)を行います。

レポート モデルに格納されたレポートには、2 つ以上のレポート担当者の詳細を含めることができます。

データベースでは、値は次のようになります。

Report テーブルと Reportperson テーブルの値が画像のようになっていれば、Report モデルは

レポート モデル

レポーターモデルは

報告者モデル

ここでは、レポートに2人のレポート担当者詳細が含まれています。この場合、検索を実行すると、同じレポートが2回表示されます。レポート担当者詳細の数によって、同じレポートが何度も表示される回数が異なります。

これを処理する方法を知りたいのですが、Reportperson テーブルから名前検索を使用しているため、これが発生しています。問題はこれから発生しreport=reports.filter(....| Q(reportperson__name__icontains=search_keyword))ています。助けが必要です。

4

1 に答える 1