Django filter exclude foreign key

前端 未结 1 1519
你的背包
你的背包 2021-01-13 16:55

I\'m creating a news website which has two models:

  1. News
  2. BestNews.

BestNews has a foreign key of News.

News stand

相关标签:
1条回答
  • 2021-01-13 17:46
    all_news = News.objects.all().order_by('-pk')
    

    to

    all_news = News.objects.filter(select_news__isnull=True).order_by('-pk')
    

    free advice:

    change

    bestnews1 = Best.objects.filter(select_reason="左一")[0].select_news
    

    to

    bestnews1 = Best.objects.filter(select_reason="左一").first()
    bestnews1_new = None if bestnew1 is None else bestnews1.select_news
    return render(request, 'index.html', {
                'all_news': news,
                 'bestnews1_new':bestnews1_new,
                 'bestnews2_new':bestnews2_new,
            })
    
    0 讨论(0)
提交回复
热议问题