pritam samanta
pritam samanta

Reputation: 445

How To Match Not Related Tables in Django

I have two tables which are not related-

class DedupeDataModel(models.Model):
    clean_name = models.CharField(max_length=200)

class SalesforceDataModel(models.Model):
    clean_name = models.CharField(max_length=200)
    salesforce_id = models.CharField(max_length=50)

I want to match the clean_name column in both the tables to get the salesforce_ids from SalesforceDataModel table. I have tried select_related to match the tables but since they are not related i am not getting results. Please give suggestions. Thanks in advance!!

Upvotes: 0

Views: 310

Answers (1)

Hilal Balcı
Hilal Balcı

Reputation: 176

Why dont you query them like below?

matching_salesforce_ids=[]
for each in DedupeDataModel.objects.all():
   models=SalesforceDataModel.objects.filter(clean_name=each.clean_name)
   if len(models)>0:
     matching_salesforce_ids.append(models.first().salesforce_id)
   

Im assuming you have one SalesforceDataModel for each clean_name.

--EDIT---

You can also try this

clean_names=DedupeDataModel.objects.all().values('clean_name')
salesforceids=SalesforceDataModel.objects.filter(clean_name__in=clean_names).values('salesforce_id')

Sorry, I dont know why I havent thought about this one before!

Upvotes: 1

Related Questions