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.
manage.py dbshell? That would find out if it's a permission problem I guess.