I have the following models.
class UserBudget(models.Model):
cat = models.ForeignKey('preference.Category', on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
budget = models.DecimalField(default=0.00, decimal_places=2, max_digits=20)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Category(models.Model):
name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
is_active = models.BooleanField(default=True)
class UserCategory(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
# cat_style_id = models.ForeignKey('preference.CategoryStyle', on_delete=models.CASCADE)
cat = models.ForeignKey('preference.Category', null=True, on_delete=models.CASCADE)
# style = models.ForeignKey('preference.Style', null=True, on_delete=models.CASCADE)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
I want to perform the following query.
select uc.id as cat_id, uc.user_id cat_user, ub.user_id as budget_user, ub.cat_id as budget_cat from user_categories as uc
left outer join user_budget as ub on uc.id = ub.cat_id
where uc.user_id = 2
Expecting follwoing result