How to write below sql query using Django queryset?

Viewed 24

I have tried many solutions but could not find any luck, and also is django queryset faster than the raw sql join queries?

Models -

class S2BotCategoriesMappings(models.Model):
    id = models.IntegerField(primary_key=True)
    id_s2_bot = models.ForeignKey(S2Bot, models.DO_NOTHING, db_column='id_s2_bot', blank=True, null=True)
    id_s2_bot_categories = models.ForeignKey(S2BotCategories, models.DO_NOTHING, db_column='id_s2_bot_categories', blank=True, null=True)

    class Meta:
        managed = False
        db_table = 's2_bot_categories_mappings'


class S2UserSubscription(models.Model):
    id = models.IntegerField(primary_key=True)
    id_s2_users = models.ForeignKey('S2Users', models.DO_NOTHING, db_column='id_s2_users', blank=True, null=True)
    id_s2_bot = models.ForeignKey(S2Bot, models.DO_NOTHING, db_column='id_s2_bot', blank=True, null=True)

    class Meta:
        managed = False
        db_table = 's2_user_subscription'
query = """
            SELECT
               s2_user_subscription.id_s2_users,
               s2_user_subscription.id_s2_bot,
               s2_bot_categories_mappings.id_s2_bot_categories
            FROM  s2_user_subscription
            LEFT JOIN s2_bot_categories_mappings 
            ON s2_user_subscription.id_s2_bot = s2_bot_categories_mappings.id_s2_bot;
        """
1 Answers
Related