Search code examples
pythondjangomodels

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()

How can I check that the office_list name equals to the tg_list name? I want to check if any of the office_list.name == any of the tg_list.name


Solution

  • if you want

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

    you can do simple query with exists:

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