Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于Django queryset跨多个模型和字段搜索的问题_Python_Sql_Django_Django Models_Django Views - Fatal编程技术网

Python 关于Django queryset跨多个模型和字段搜索的问题

Python 关于Django queryset跨多个模型和字段搜索的问题,python,sql,django,django-models,django-views,Python,Sql,Django,Django Models,Django Views,我的Django代码中有以下模型 class Card(models.Model): card_name = models.CharField(max_length=255) geography_choices = [('Africa', 'Africa'), ('Asia', 'Asia'), ('Australia', 'Australia'), ('Europe', 'Europe'), ('North America', 'North America'), ('Sout

我的Django代码中有以下模型

class Card(models.Model):
    card_name = models.CharField(max_length=255)

    geography_choices = [('Africa', 'Africa'), ('Asia', 'Asia'), ('Australia', 'Australia'), ('Europe', 'Europe'), ('North America', 'North America'), ('South America', 'South America')]
    geography = models.CharField(max_length=255, choices=geography_choices, default='North America')

    type_choices = [('Creature', 'Creature'), ('Spell', 'Spell'), ('New Type', 'New Type'),('Undecided', 'Undecided')]
    card_type = models.CharField(max_length=255, choices=type_choices, default='Creature')


class Article(models.Model):

    title = models.CharField(max_length=255)

    body = models.TextField()

    author = models.ForeignKey(
        get_user_model(),
        on_delete=models.CASCADE, 
    )

    entry_date_time = models.DateTimeField(default=timezone.now, null=True)

    class Meta:
        ordering = ['-entry_date_time']

    score_choices = [(-5,'-5'), (-4, '-4'), (-3,'-3'), (-2, '-2'), (-1,'-1'), (0,'0'),
    (1,'1'), (2,'2'), (3,'3'), (4,'4'), (5,'5')]
    score = models.FloatField(choices=score_choices, default=0)


    read_through_choices = [(-1, 'Competitive'), (0, 'No Effect'), (1, 'Industry-Wide')]
    read_through = models.IntegerField(choices=read_through_choices, default=0)


    cardvar = models.ForeignKey(Card, on_delete=models.CASCADE, null=True)
在我的视图中,我还具有以下查询功能作为搜索功能:

def get_queryset(self): #new 
        query =self.request.GET.get('q')
        results = Article.objects.filter(
            Q(cardvar__card_name__iexact=query)
        ).order_by('cardvar', '-entry_date_time')
        return results 

但是,此搜索函数仅返回其“卡片主题”为搜索主题的文章。我想做的是搜索一个卡片名(cardvar),不仅返回带有该cardvar Foreignkey的文章,而且返回a)与card_var具有相同卡片类型的文章,以及b)文章分数不等于零的文章。我想通过搜索特定的卡片名称来返回这些结果(即文章)。我希望这是清楚的。任何帮助都将不胜感激。

您应该首先进行查询,并获取搜索到的
卡片名称的
卡片类型
,然后在查询中使用它。它类似于以下内容:

query=self.request.GET.GET('q'))
card\u dict=card.objects.values('card\u type').get(card\u name=query)
结果=Article.objects.filter(
Q(cardvar_uuuucard_uuuname_uuuuuuuixact=query)|(Q(cardvar_uuuuucard_utype=card_udict['card_type']&~Q(分数=0))
).order_by('cardvar','-输入日期时间')

谢谢!目前我的get\u queryset函数上面有这样一个函数:model=文章上下文\u object\u name='Article\u list'template\u name='search\u results.html',它“导入”文章模型。如何让视图也“知道”要使用卡片模型吗?否,
model=Article
用于此视图类,我假定它是一个
ListView
它与导入文章模型无关。如果卡片和文章模型位于同一文件中,请在
views.py
文件开头搜索导入文章并添加
Card
nex我不同意