通过ID过滤查询到Django中的另一个表

社会演员多 python 190

原文标题Filter query through an ID to another table in Django

假设我有这个查询

表格1

cardId = 1
table_1 = card_tbl.ocabjects.get(id=cardId).values('category1_id')

表 1 的输出是<QuerySet [{'category1_id': 1}]>

接下来是我要匹配table 1totable 2的值

表 2

table_2 = category1_tbl.objects.filter(Q(id=table_1))

table 2包含的列是name,我只想通过匹配来自不同的ID来获取结果名称table例如jason

需要帮忙

原文链接:https://stackoverflow.com//questions/71509783/filter-query-through-an-id-to-another-table-in-django

回复

我来回复
  • Waqas的头像
    Waqas 评论

    我无法得到您的确切观点,但如果您想根据表 1 的 ID 从表 2 中过滤。

    可以这样做

    id_table1 = [id for id in table 1]
    table_2 = category1_tbl.objects.filter(id__in=id_table1)
    
    2年前 0条评论