如何检查 Django 中的两个模型是否相等?

How can I check if two models equal each other in Django?

models.py:

class office_list(models.Model):
    name = models.CharField(max_length= 100)
    num_of_pax = models.IntegerField()

class tg_list(models.Model):
    name = models.CharField(max_length= 100)
    num_of_pax = models.IntegerField()

如何检查 office_list 名称是否等于 tg_list 名称? 我想检查是否有任何 office_list.name == 任何 tg_list.name

来自Django doc :

比较两个模型实例,只需使用标准Python比较运算符,双等号:==。在幕后,比较两个模型的主键值。

您也可以在 python 中使用 __eq__

也见 python docs

如果你想要

any of the office_list.name == any of the tg_list.name

您可以使用 exists 进行简单查询:

names = tg_list.objects.values_list('name', flat=True)
office_list.objects.filter(name__in=names).exists()