'Comparing two fields of different models without using foreign key in django
This is regarding CRM software.
I have two models profile_candidate and Agent,
profile_candidate contains some 20 fields and Reference_Mobile is one of them.
Agent is the model which contains the names of all the users who work as agents with the field name user.
Here what I need to do is...
I need to compare the field Reference_Mobile of each profile_candidate with all the users available in Agent model, and I need to retrieve the objects of profile_candidate(model) where the Reference_Mobile is not matching with any of the users in the Agent Model.
Note: Without using foreign key for Reference_Mobile field from the Agent model, because I cannot display all the users to the candidates while filling the form of profile_candidate.
This is the problem I have been facing for the last two days, I am beginner of Django and python too.
I also tried many other different ways but I could not find the proper output.
I hope I explained clearly the problem. Please anyone help me out in achieving this..
my views.py:
def lead_unassigned(request):
team_values = profile_candidate.objects.exclude(Reference_Mobile = Agent.user)
return render(request, 'leads/lead_list_unassigned.html',
{'team_values':team_values})
Thanking you in Advance,
Dinesh Babu
Solution 1:[1]
If the Reference_Mobile field of the profile_candidate model is not the same object type as Agent.user then the exclude will not be able to compare the two accurately. Double check that the model field types are the same, if they are not then you may be comparing a sub-field within Reference_Mobile, like a username or other type.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mattdood |
