I'm working with a legacy database so I have to set managed=False in the model. Here's the 3 related tables:
class Branches(models.Model): name = models.CharField(max_length=128) branchpoint_str = models.CharField(max_length=255) dev_lead_id = models.IntegerField(blank=True, null=True) source = models.CharField(max_length=255) state = models.CharField(max_length=255) kind = models.CharField(max_length=255) desc = models.TextField(blank=True, null=True) approved = models.IntegerField() for_customer = models.IntegerField() deactivated_at = models.DateTimeField(blank=True, null=True) created_at = models.DateTimeField(blank=True, null=True) updated_at = models.DateTimeField(blank=True, null=True) codb_id = models.IntegerField(blank=True, null=True) pm_lead_id = models.IntegerField(blank=True, null=True) version = models.CharField(max_length=20, blank=True, null=True) path_id = models.IntegerField(blank=True, null=True) branchpoint_type = models.CharField(max_length=255, blank=True, null=True) branchpoint_id = models.IntegerField(blank=True, null=True) class Meta: managed = False db_table = 'branches' verbose_name_plural = 'Branches' class Projects(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=40, primary_key=True) status = models.CharField(max_length=255) platform = models.CharField(max_length=255) enabled = models.IntegerField() path = models.CharField(max_length=128, blank=True, null=True) tag_prefix = models.CharField(max_length=64, blank=True, null=True) created_at = models.DateTimeField(blank=True, null=True) updated_at = models.DateTimeField(blank=True, null=True) codb_id = models.IntegerField(blank=True, null=True) template = models.CharField(max_length=64, blank=True, null=True) image_path = models.CharField(max_length=128, blank=True, null=True) repository_id = models.IntegerField(blank=True, null=True) number_scheme = models.CharField(max_length=32) special_dir = models.CharField(max_length=32, blank=True, null=True) project_family_id = models.IntegerField() class Meta: managed = False db_table = 'projects' verbose_name_plural = 'projects' class BranchesProjects(models.Model): # project_id = models.IntegerField() # branch_id = models.IntegerField() project = models.ForeignKey(Projects, on_delete=models.CASCADE) branch = models.ForeignKey(Branches, on_delete=models.CASCADE) class Meta: managed = False db_table = 'branches_projects' I have been able to do the join using raw(). However, the return object is rawqueryset. What I want is queryset so that I can use django-filter to process it. My current raw sql is like this: Branches.objects.raw( '''SELECT br.id, br.name, br.created_at, br.updated_at, br.branchpoint_str, br.source FROM branches as br LEFT JOIN branches_projects as bp ON br.id = bp.branch_id WHERE bp.project_id = "%s" AND source != "other" ORDER BY updated_at DES''' My question is, is there a way to achieve the same result using Django's queryset? I've also explored the idea of using the extra() in django extra <https://docs.djangoproject.com/en/1.11/ref/models/querysets/#django.db.models.query.QuerySet.extra> but it doesn't really work for me. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1581448d-bb69-4e85-a35c-62d0bfad88ed%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.