Django Order by based on field in unrelated model
I have two models in my Django Project. The first one is UserProfile and the second one is Interests:
class UserProfile(models.Model):
user_model_for_profile = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
first_name = models.CharField(max_length = 25)
middle_name = models.CharField(max_length = 25)
last_name = models.CharField(max_length= 50)
gender = models.CharField(choices = gender_choices, max_length = 30)
profile_created_by = models.CharField(choices = profile_creation_choices, max_length = 30)
date_of_birth = models.DateField()
time_of_birth = models.TimeField()
Height = models.IntegerField()
#Day of birth needed?
Country_of_birth = models.CharField(choices = Countries_choices, max_length = 30)
State_of_birth = models.CharField(choices = State_choices, max_length = 40)
City_of_birth = models.CharField(max_length = 30)
Mobile_number = models.CharField(
max_length=10, # Adjust based on your needs
validators=[RegexValidator(regex=r'^[6-9]\d{9}$')],
unique = True
)
Password = models.CharField(max_length= 50)
Religion = models.CharField(choices = Religion_choices, max_length = 30)
Sub_community = models.CharField(choices = Sub_community_choices, max_length = 30)
Nukh = models.CharField(max_length = 30)
Verified = models.BooleanField(default = True)
class Interests(models.Model):
interest_sent_by = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='relationship5')
interested_in = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='relationship6')
How do I sort my UserProfile objects based on Interests(specifically if interested_in == request.user.id)?