0

Am trying to build a complex queryset whereby am joining with other tables. What is buzzling me here is that am able to see the query when i print the queryset.query but as i call it from my code its returning no result.

class CarManager(models.Manager):
    def get_query_set(self):
        return super(CarManager, self).get_query_set().filter(version_to__isnull=True)

    def my_cars(self, language, user):

        qs=self.extra(
            select = {
                'make_display' : '`%s`.`%s`' % (ModelLookUpI18n._meta.db_table, ModelLookUpI18n._meta.get_field('make_display').column),
                'model_display' : '`%s`.`%s`' % (ModelLookUpI18n._meta.db_table, ModelLookUpI18n._meta.get_field('model_display').column),
                'trim_display'  : '`%s`.`%s`' % (ModelLookUpI18n._meta.db_table, ModelLookUpI18n._meta.get_field('trim_display').column),
                },
            tables = [
                '`%s`' % ModelLookUpI18n._meta.db_table,
            ],
            where = [
                '`%s`.`%s` = `%s`.`%s`' % (ModelLookUpI18n._meta.db_table, ModelLookUpI18n._meta.get_field('model').column, ModelLookup._meta.db_table, ModelLookup._meta.get_field('id').column),
                "`%s`.`%s`='%s'" % (ModelLookUpI18n._meta.db_table, ModelLookUpI18n._meta.get_field('language').column, language)
            ]
        ).select_related().filter(created_by=user).order_by('-created_at')

        print qs.query # prints the SQL statement
        print qs # always prints []
        return qs

When I copy the query printed in the console and paste it into mySQL terminal I 21 records and it looks very normal to me. Yet, the queryset is always empty, any ideas on how to debug further?

Oh, I even monitored the MYSQL execution log to make sure what is going to the engine is matching what is printed by qs.query and its the same.

2
  • 1
    Interresting, did you try to paste the query in manage.py dbshell ? That would find out if it's a permission problem I guess. Commented Nov 2, 2012 at 15:05
  • i found the problem, when i the alias name cannot be the same name as the field in the select params. I had to replace "make_display" with "make_i18n" for example in order for it to work Commented Nov 2, 2012 at 15:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.